也许您混淆了时间点(即NSDate对象)和您所在位置的时间点(如当地时间)。
If you print a NSDate (like NSLog(@"%@", [NSDate date]);
which invokes [date description]
) the date representation that is printed is in UTC timezone (+0000) (at least it is on my computer).
So as long as you don t live in an area that uses UTC the date printed by [date description]; is always "wrong". But wrong only means that its representation is not the same representation as the clock in your office. The date (as in point in time) is still correct.
当您使用localizedStringFromDate:dateStyle:timeStyle:
时,您正在打印本地时区中的日期。
NSDate *date = [NSDate date];
NSLog(@"%@", date);
NSLog(@"%@", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
在我的电脑上,结果是:
2011-02-12 08:32:10.658 x[75647:207] Date: 2011-02-12 07:32:10 +0000
2011-02-12 08:32:10.661 x[75647:207] Date: Saturday, February 12, 2011 8:32:10 AM Central European Time
打印的字符串不同,但NSDate对象仍然相同。这就是为什么在向用户显示日期时必须使用NSDateFormatters的原因。因为同一个时间点在世界不同的地方看起来不同。
但只有三个地方的UTC格式的日期在未来是一小时,所以如果你不住在绿地、佛得角或阿佐雷斯,我可能完全错了,你的NSDate对象也有问题。
编辑:出于好奇,我再次阅读了关于[日期描述]的文档。上面写着
A string representation of the
receiver in the international format
YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM
represents the time zone offset in
hours and minutes from GMT (for
example, “2001-03-24 10:45:32 +0600”).
So I don t know why the date at my computer is printed in GMT timezone. It might be in another timezone at your computer.
But still, it s only the representation, the date is still the same.