2010-10-10 17 views

Odpowiedz

29
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 
[dateFormatter setTimeZone:gmt]; 
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; 
[dateFormatter release]; 
+0

działa jak czar ... dzięki! :) – unicornherder

+5

Jak to przekształca GMT na czas lokalny? Uważam, że robi się coś przeciwnego i zamienia lokalne na GMT? – willhblackburn

1
NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp]; 
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 
NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0]; 
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; 
[dateFormatter setTimeZone:destinationTimeZone]; 
NSString *localTimeString = [dateFormatter stringFromDate:destinationDate]; 
-2
NSDate *now = [NSDate date]; 
NSLog(@"%@", [now dateWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" 
       timeZone:[NSTimeZone timeZoneWithName:@"GMT"]]); 
+0

Komunikat '- (NSString *) dateWithCalendarFormat: timezone:' nie istnieje w dokumentacji Apple. –

+0

powoduje to błąd. Czy testujesz go? –

17
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate]; 
+0

To jest świetne. To rozwiązuje mój problem –

+0

** Swift **: 'let localDate = NSDate (timeInterval: NSTimeInterval (NSTimeZone.systemTimeZone(). SecondsFromGMT), sinceDate: date) – Husam

Powiązane problemy