如何将UTCdatestring转换为本地时间(systemTimeZone)

inputstring:2012年6月14日 – 01:00:00 UTC

输出本地string:2012年6月13日 – 21:00:00 EDT

我喜欢从中获得抵消

NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; NSLog(@"Time Zone: %@", destinationTimeZone.abbreviation); 

任何build议?

这应该做你所需要的:

 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz"; NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"]; fmt.timeZone = [NSTimeZone systemTimeZone]; NSString *local = [fmt stringFromDate:utc]; NSLog(@"%@", local); 

请注意,您的示例不正确:在UTC时间是6月14日上午1点,在EST,8 PM标准或9 PM夏令时仍然是6月13日。 在我的系统上这个程序打印

 Jun 13, 2012 - 21:00:00 EDT 
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"MMMM d, yyyy - HH:mm:ss zzz"; // format might need to be modified NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; [dateFormatter setTimeZone:destinationTimeZone]; NSDate *oldTime = [dateFormatter dateFromString:utcDateString]; NSString *estDateString = [dateFormatter stringFromDate:oldTime]; 

这从格林威治标准时间转换到本地时间,你可以修改一下UTC时间

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm"; NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; [dateFormatter setTimeZone:gmt]; NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]]; [dateFormatter release]; 

取自iPhone:NSDate将GMT转换为当地时间

Swift 3

 var dateformat = DateFormatter() dateformat.dateFormat = "LLL d, yyyy - HH:mm:ss zzz" var utc: Date? = dateformat.date(fromString: "June 14, 2012 - 01:00:00 UTC") dateformat.timeZone = TimeZone.current var local: String = dateformat.string(from: utc) print(local)