2013-03-25 13 views
6

Ho appena notato che NSDate *nowDate = [NSDate date]; mi dà GMT + 0 Ora e non l'ora locale. Quindi in pratica sul mio iPad sono le 13:00 e l'uscita di questo codice è 12:00.Come ottenere l'ora locale su iOS

Come si ottiene l'ora locale correttamente?

+0

Prova questo link, permette di ottenere il fuso orario locale. http://buildingmyworld.wordpress.com/2011/04/13/get-local-time-from-iphone-local-nsdate/ –

+0

Come si ottiene "l'output di questo codice"? Stai usando un NSDateFormatter per ottenere una stringa o stai semplicemente 'NSLog'ging l'oggetto NSDate? – colincameron

risposta

28

Provalo!

NSDate* sourceDate = [NSDate date]; 

NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];//use `[NSTimeZone localTimeZone]` if your users will be changing time-zones. 

NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; 
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 

NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease]; 

Vi darà l'ora in base al fuso orario del sistema corrente.

+11

perché è così dannatamente complicato. -1 per iOS! –

+4

non è complicato. Il tempo è il tempo è il tempo. Il formato del tempo è difficile perché gli esseri umani hanno creato fusi orari. In modo che le 6:00 del pomeriggio sarebbero in giro per il mondo. – John

+0

Questo ti darà un punto nel tempo 'x ore fa o in futuro, non l'ora corrente. – colincameron

4
// get current date/time 
NSDate *today = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings 
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
NSString *currentTime = [dateFormatter stringFromDate:today]; 
[dateFormatter release]; 
NSLog(@"%@",currentTime); 
3
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
[calendar setTimeZone:[NSTimeZone localTimeZone]]; 
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; 
NSDate *d = [calendar dateFromComponents:dateComponents]; 
6

NSDate non si preoccupa di fusi orari. Registra semplicemente un momento nel tempo.

è necessario impostare il fuso orario locale quando si utilizza il NSDateFormatter per ottenere una rappresentazione di stringa di data:

NSDate *date = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; 
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Set date and time styles 
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]]; 
NSString *dateString = [dateFormatter stringFromDate:date]; 
+0

e poi? Devo convertire NSSTring in NSDate nuovamente? Non sembra molto efficiente –

+0

Se hai bisogno di un NSDate, usa semplicemente '[Data NSDate]' - non interessa i fusi orari. Vi suggerisco di leggere la Guida alla programmazione di data e ora - https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.html#//apple_ref/doc/uid/10000039i – colincameron

+0

Questo è il problema Ho bisogno di un oggetto NSDate a cui interessare;) –

Problemi correlati