将 UTC 时间转换为系统时间变为空值


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);

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

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);

如果有效,请告诉我。

最新更新