将UTC时间转换为系统时间为null

时间:2017-07-04 06:13:18

标签: ios objective-c nsdate nsdateformatter

 NSString *strUTCTime=@"2017-07-06T10:00:00";

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateFormat = @"yyyy-MM-ddTHH:mm:ss";

 NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
 [dateFormatter setTimeZone:destinationTimeZone];

 NSDate *oldTime = [dateFormatter dateFromString:strUTCTime];

 NSString *estDateString = [dateFormatter stringFromDate:oldTime];
 NSLog(@"Local time is ===> %@",estDateString);

1 个答案:

答案 0 :(得分:1)

以下是经过工作和测试的代码。

NSString *strUTCTime=@"2017-07-06T10:00:00";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en-US"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

NSDate *oldTime = [dateFormatter dateFromString:strUTCTime];

NSLog(@"UTC time is ===> %@",oldTime);

NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
[dateFormatter setTimeZone:destinationTimeZone];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

NSString *estDateString = [dateFormatter stringFromDate:oldTime];
NSLog(@"Local time is ===> %@",estDateString);

如果有效,请告诉我。