返回将来一小时的日期 [英] returns a date an hour in the future

查看:102
本文介绍了返回将来一小时的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 [NSDate date] 获取当前日期/时间。返回的值是将来一小时。我检查了我的手机位置和时间设置,它们是正确的。

I'm getting the current date/time using [NSDate date]. The value returned is an hour in the future. I've check my phones location & time settings and they are correct.

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

I can display the correct date and time as a string using the code below.

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

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

But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using -

[date dateByAddingTimeInterval:interval]

我意识到我的问题类似于已经问过的这个问题,但没有一个答案符合我的需要。提前致谢!

I realise my question is similar to this one already asked but none of the answers suit my needs. Thanks in advance!

init过去的一个小时返回日期?

推荐答案

也许你很困惑时间点(即NSDate对象)和您所在位置的时间点(即当地时间)。

Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).

如果您打印NSDate(如<$ c) $ c> NSLog(@%@,[NSDate date]); 调用 [date description] )打印的日期表示是UTC时区(+0000)(至少它在我的电脑上)。

所以只要你不住在使用UTC的区域,由[日期描述]打印的日期;永远是错误的。但错误只意味着它的表现形式与你办公室的时钟不同。日期(如时间点)仍然正确。

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:您在当地时区打印日期。

When you use localizedStringFromDate:dateStyle:timeStyle: you are printing the date in your local timezone.

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的原因。因为同一时间点在世界不同的地方看起来不同。

the printed strings are different, but the NSDate object is still the same. That's why you have to use NSDateFormatters when you show a date to the user. Because the same point in time looks different on different places of the world.

但是,将来只有三个地方的UTC格式日期是一小时,所以如果你不这样做生活在格陵兰岛,佛得角或亚速尔群岛我可能完全错了,你的NSDate对象有问题。

But there are only three places where an UTC formatted date would be one hour in the future, so if you don't live in greenland, cape verde or on the azores I might be totally wrong and there is something wrong with your NSDate objects.

编辑:出于好奇,我再次阅读了有关[日期描述]的文档。它说

Out of curiosity I read the documentation about [date description] again. And it says


国际格式的
接收器的字符串表示形式
YYYY-MM-DD HH: MM:SS±HHMM,其中±HHMM
表示格林威治标准时间
小时和分钟的时区偏移(以b $ b为例,2001-03-24 10:45:32 +0600 )。

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").

所以我不知道为什么计算机上的日期是用GMT时区打印的。它可能在你计算机的另一个时区。

但是,它只是表示,日期仍然相同。

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.

这篇关于返回将来一小时的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆