English 中文(简体)
返回一小时后的日期
原标题:returns a date an hour in the future

我正在使用[NSDate date]获取当前日期/时间。返回的值是未来的一个小时。我已经检查了我的手机位置&;时间设置,并且它们是正确的。

我可以使用下面的代码将正确的日期和时间显示为字符串。

[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle 
                                                       timeStyle:NSDateFormatterShortStyle]

但我需要它作为日期对象返回正确的日期/时间,因为我使用它来计算预计到达时间-

[date dateByAddingTimeInterval:interval]

我意识到我的问题与已经问过的问题相似,但没有一个答案符合我的需求。提前感谢!

init]返回过去一小时的日期

最佳回答

也许您混淆了时间点(即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.

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签