NSDate/NSDateFormatter在iPhone iOS 4.3上返回GMT [英] NSDate / NSDateFormatter returning GMT on iPhone iOS 4.3
问题描述
为什么这段代码会给我格林尼治标准时间(GMT)? (我在美国山区时间)
Why would this code be giving me GMT? (I am in US Mountain Time)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSDate *now = [NSDate date];
NSString *storeTime = [dateFormatter stringFromDate:now];
推荐答案
NSDate
代表具体的时间点,与时区无关.换句话说,NSDate
没有时区.时区仅在您想要向用户显示日期时才相关.因此,山区时间的9:30 pm是(格林尼治标准时间)上午3:30(+1天)(假设时差为6小时).
An NSDate
represents a concrete point in time, regardless of the timezone. Put another way, an NSDate
does not have a timezone. Timezones are only relevant when you want to display the date to the user. So 9:30pm in Mountain Time is 3:30am (+1 day) in GMT (assuming a 6 hour time difference).
NSDate
由于没有时区,因此在生成人类可读版本时必须选择一个作为其-description
返回.为简单起见,它总是返回以GMT时区为格式的日期.如果希望将日期格式设置为其他时区,则可以设置NSDateFormatter
的-timezone
属性,然后使用-stringFromDate:
方法将日期转换为字符串.
NSDate
, since it does not have a timezone, must pick one when producing a human-readable version to return as its -description
. To make things simple, it always returns a date formatted in the GMT time zone. If you would like the date formatted to be in a different timezone, you can set the -timezone
property of an NSDateFormatter
, and then convert the date into a string using the -stringFromDate:
method.
这篇关于NSDate/NSDateFormatter在iPhone iOS 4.3上返回GMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!