2013-08-03 10 views
5

Ich versuche, die folgende Zeitzone Gerät lokale Zeitzone zu konvertieren:iOS: lokale UTC-Zeitzone zu Zeitzone Gerät konvertieren

2013-08-03T05:38:39.590Z 

Bitte lassen Sie mich wissen, wie es zu lokalen Zeitzone zu konvertieren.

Wie mache ich das?

+0

möglich Duplikat von [Wie die Zeit in die Zeitzone des iPhone-Gerät konvertieren?] (Http://stackoverflow.com/questions/1081647/how-to-convert-time-to-the-timezone-of-the-iphone-device) –

Antwort

11

Zeitzonen können auf NSDateFormatter angewendet werden, aus denen Sie NSDate-Variablen generieren können, die durch die Zeitdifferenz differenziert sind.

NSString* input = @"2013-08-03T05:38:39.590Z"; 
NSString* format = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; 

// Set up an NSDateFormatter for UTC time zone 
NSDateFormatter* formatterUtc = [[NSDateFormatter alloc] init]; 
[formatterUtc setDateFormat:format]; 
[formatterUtc setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 

// Cast the input string to NSDate 
NSDate* utcDate = [formatterUtc dateFromString:input]; 

// Set up an NSDateFormatter for the device's local time zone 
NSDateFormatter* formatterLocal = [[NSDateFormatter alloc] init]; 
[formatterLocal setDateFormat:format]; 
[formatterLocal setTimeZone:[NSTimeZone localTimeZone]]; 

// Create local NSDate with time zone difference 
NSDate* localDate = [formatterUtc dateFromString:[formatterLocal stringFromDate:utcDate]]; 

NSLog(@"utc: %@", utcDate); 
NSLog(@"local: %@", localDate); 

[formatterUtc release]; 
[formatterLocal release]; 
1

Versuchen wie folgt aus:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; 
    NSDate* utcTime = [dateFormatter dateFromString:@"2013-08-03T05:38:39.590Z"]; 
    NSLog(@"UTC time: %@", utcTime); 

    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; 
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"]; 
    NSString* localTime = [dateFormatter stringFromDate:utcTime]; 
    NSLog(@"localTime:%@", localTime); 

Hoffe, dass es Ihnen hilft ....

+0

Yessss es tat. Vielen Dank Kumpel –

Verwandte Themen