如何在iOS上获取本地时间 [英] How to get local time on iOS
本文介绍了如何在iOS上获取本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚注意到 NSDate * nowDate = [NSDate date];
给我GMT + 0时间而不是当地时间。所以基本上在我的iPad上是13:00,这段代码的输出是12:00。
I just noticed that NSDate *nowDate = [NSDate date];
gives me GMT+0 Time and not the local time. So basically on my iPad it's 13:00 and the output of this code is 12:00.
如何正确地获得当地时间?
How do I get local time properly?
推荐答案
试一试!
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];//use `[NSTimeZone localTimeZone]` if your users will be changing time-zones.
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
它会根据当前系统时区给你时间。
It will give you the time according to the current system timezone.
这篇关于如何在iOS上获取本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文