NSDate休息5小时 [英] NSDate is 5 hours off
问题描述
我运行以下代码:
NSDate *现在= [NSDate日期];
NSDate *now = [NSDate date];
NSLog(@"now: %@", now);
并获取:
2011-09-16 16:14:16.434 iSavemore [1229:7907]现在:2011-09-16 21:14:16 +0000
2011-09-16 16:14:16.434 iSavemore[1229:7907] now: 2011-09-16 21:14:16 +0000
您可以看到我在16:14:16(下午4:14)运行此程序,但NSDate返回21:16:16(下午9:14!).这是Xcode4问题还是NSDate问题?
As you can see i'm running this at 16:14:16 (4:14 pm) but NSDate is returning 21:16:16 (9:14 pm!). Is this an Xcode4 issue or NSDate issue?
推荐答案
NSDate默认为世界时区(又名GMT).
NSDate defaults to the Universal timezone (aka GMT).
我猜你在美国东部海岸的某个地方,比世界协调时间晚5小时.
I'm guessing you're somewhere on the East Coast, 5 hours behind UTC.
尝试将其添加到您的日期格式器中...
Try adding this to your date formatter...
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
...,您应该会看到当地时间.
...and you should see your local time.
如果要使用指定的语言环境而不是"currentLocale",请为相关语言环境创建一个NSLocale.
If you want to use a specified locale, rather than 'currentLocale', create a NSLocale for the relevant locale.
NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLoc];
...实际上是美国(因此可能不是中部地区).
...actually that's US (so possibly not Central).
可以在此处找到更具体的时区帮助...
More specific timezone help can be found here...
但是,如果您想显示到期时间,您是否还不想在用户的currentLocale中显示它?
However, if you want to show expiry time, wouldn't you still want it in the user's currentLocale?
这篇关于NSDate休息5小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!