當我轉換的時候從我的Xcode iPhone模擬器我的代碼工作正常,輸出,因爲它顯示在這裏:iPhone:轉換GMT爲本地時間
2014年2月12日18點11分52秒+0000
本地時區(歐洲/倫敦(GMT)偏移0)
但是當我嘗試使用我的iPhone 5我的應用程序的輸出變爲
1970-01-01 12:00:00 +0000
本地時區(歐洲/倫敦(GMT)偏移0)
我使用xcode的5.0和iOS 7
-(NSDate *)convertGMTtoLocal:(NSString *)gmtDateStr {
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *gmt = [NSTimeZone systemTimeZone];
[formatter setTimeZone:gmt];
NSDate *GMTTime = [formatter dateFromString:gmtDateStr];
NSTimeZone *tz = [NSTimeZone localTimeZone];
NSLog(@"%@",tz);
NSInteger seconds = [tz secondsFromGMTForDate: GMTTime];
NSLog(@"%ld",(long)seconds);
NSLog(@"%@",[NSDate dateWithTimeInterval: seconds sinceDate: GMTTime]);
return [NSDate dateWithTimeInterval: seconds sinceDate: GMTTime];
}
謝謝,
http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal -with-the-nsdateformatter-locale-feature –
感謝問題解決:) – user3116050