从[NSDate date]开始约会几个小时 [英] Getting date from [NSDate date] off by a few hours

查看:127
本文介绍了从[NSDate date]开始约会几个小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

NSDate *date = [NSDate date];

获取日期,但我获得的日期为2小时。

for getting the date, but the date I get is off by 2 hours.

推荐答案

NSDate 对象没有时区;它们表示绝对的时间。然而,当你问一个描述(通过在 NSLog 中打印,例如)一个时区。最合理的默认选择是GMT。如果您不是自己在GMT,日期似乎不正确,以您自己的偏移量。

NSDate objects don't have time zones; they represent an absolute moment in time. However, when you ask one for its description (by printing it in an NSLog, e.g.), it has to pick a time zone. The most reasonable "default" choice is GMT. If you're not in GMT yourself, the date will seem to be incorrect, by the amount of your own offset.

您应该总是使用 NSDateFormatter ,在显示日期之前将其 timezone 设置为yours。

You should always use an NSDateFormatter, setting its timezone to yours, before displaying a date.

这篇关于从[NSDate date]开始约会几个小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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