返回未来一小时后的日期
我使用 [NSDate date]
获取当前日期/时间。返回的值是未来一个小时。我已经检查了我的手机位置&时间设置并且它们是正确的。
我可以使用下面的代码将正确的日期和时间显示为字符串。
[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle]
但我需要它返回正确的日期/时间作为日期对象,因为我使用它来计算预计到达时间 -
[date dateByAddingTimeInterval:interval]
我意识到我的问题与已经提出的问题类似,但没有一个答案适合我的需要。提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您混淆了时间点(即 NSDate 对象)和您所在位置的时间点(即您的当地时间)。
如果打印 NSDate(例如调用
[date description]
的NSLog(@"%@", [NSDate date]);
),则打印的日期表示形式为UTC 时区 (+0000)(至少在我的电脑上是这样)。因此,只要您不住在使用 UTC 的地区,[日期描述] 打印的日期;总是“错”的。但错误仅意味着它的表示与您办公室中的时钟的表示不同。日期(如时间点)仍然正确。
当您使用
localizedStringFromDate:dateStyle:timeStyle:
时,您将打印本地时区的日期。在我的计算机上,这会导致:
打印的字符串不同,但 NSDate 对象仍然相同。这就是为什么当你向用户显示日期时必须使用 NSDateFormatters。因为同一时间点在世界不同地方看起来是不同的。
但是只有三个地方 UTC 格式的日期会是未来一小时,所以如果你不住在格陵兰岛、佛得角或亚速尔群岛,我可能完全错了,你的 NSDate 对象有问题。
编辑:出于好奇,我再次阅读了有关[日期描述]的文档。它说
所以我不知道为什么我的计算机上的日期是以 GMT 时区打印的。您的计算机可能位于另一个时区。
但是,这只是表象,日期仍然是一样的。
Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).
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.
When you use
localizedStringFromDate:dateStyle:timeStyle:
you are printing the date in your local timezone.at my computer this results in:
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.
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.
Edit: Out of curiosity I read the documentation about [date description] again. And it says
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.