我想將nsstring轉換爲nsdate,然後到systemtimezone。我的代碼是否正確?任何幫助讚賞。GMT時區轉換在目標c
NSString *[email protected]"2012-01-15 06:27:42";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [dateFormatter dateFromString:str];
NSDate* sourceDate = dateFromString;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-07:00"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
bottomLabel2.text=[NSString stringWithFormat:@"- %@ -",destinationDate];
代碼結果是2012-01-15 06:27:42 +0000這與源代碼相同!
「我的代碼是否正確?」不是在這裏問的問題。 http://codereview.stackexchange.com – esqew 2012-01-15 17:28:35
我不認爲原始海報是要求審查,但可能沒有明確說明問題。無論如何,我已在下面回答。 – lxt 2012-01-15 17:38:43