iOS:将UTC NSDate转换为本地时区 [英] iOS: Convert UTC NSDate to local Timezone
本文介绍了iOS:将UTC NSDate转换为本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在目标C或/和Swift中将UTC NSDate
转换为本地时区NSDate?
How do I convert a UTC NSDate
to local timezone NSDate in Objective C or/and Swift?
推荐答案
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[df_local setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
// you can also use NSDateFormatter dateFromString to go the opposite way
格式化字符串参数表:
https://waracle.com/iphone-nsdateformatter-date-formatting-table/
如果性能优先,您可能需要考虑使用 strftime
If performance is a priority, you may want to consider using strftime
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man3/strftime.3.html
这篇关于iOS:将UTC NSDate转换为本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文