我运行以下代码:
NSDate *now = [NSDate 日期];
NSLog(@"now: %@", now);
并得到:
2011-09-16 16:14:16.434 iSavemore[1229:7907] 现在:2011-09-16 21:14:16 +0000
如您所见,我在 16:14:16(下午 4:14)运行此程序,但 NSDate 返回时间为 21:16:16(晚上 9:14!)。这是 Xcode4 问题还是 NSDate 问题?
最佳答案
NSDate 默认为通用时区(又名 GMT)。
我猜你在东海岸的某个地方,比 UTC 晚 5 小时。
尝试将此添加到您的日期格式化程序...
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[NSLocale currentLocale]];
...您应该会看到本地时间。
如果您想使用指定的语言环境,而不是“currentLocale”,请为相关语言环境创建一个 NSLocale。
NSLocale *usLoc = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLoc];
...实际上那是美国(所以可能不是中部)。
可以在此处找到更具体的时区帮助...
但是,如果你想显示过期时间,难道你还不想在用户的当前语言环境中显示它吗?
关于objective-c - NSDate 关闭 5 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7450510/