如何将UTC日期字符串转换为本地时间(systemTimeZone) [英] How to convert UTC date string to local time (systemTimeZone)

查看:750
本文介绍了如何将UTC日期字符串转换为本地时间(systemTimeZone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入字符串:2012年6月14日 - 01:00:00 UTC

Input String: June 14, 2012 - 01:00:00 UTC

输出本地字符串:Jun 13,2012 - 21:00:00 EDT

Output Local String: Jun 13, 2012 - 21:00:00 EDT

我喜欢以获得从

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

有什么建议吗?

推荐答案

这应该做的事情,你需要:

This should do what you need:

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

请注意,您的示例不正确:当它在6月14日凌晨1点在UTC时,它仍然是6月美国东部时间-13,夏令时下午8点或夏令时下午9点。在我的系统上,此程序打印

Note that your example is incorrect: when it's 1 AM on June-14th in UTC, it's still June-13th in EST, 8 PM standard or 9 PM daylight savings time. On my system this program prints

Jun 13, 2012 - 21:00:00 EDT

这篇关于如何将UTC日期字符串转换为本地时间(systemTimeZone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆