将 [NSDate date] 的日期缩短几个小时 [英] Getting date from [NSDate date] off by a few hours

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

问题描述

我正在使用

NSDate *date = [NSDate date];

获取日期,但我得到的日期差了 2 小时.

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

推荐答案

NSDate 对象没有时区.它们代表了一个绝对的时刻.然而,当你询问它的 description 时(例如通过 NSLog() 打印它),它必须选择一个时区.最合理的默认"选择是 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 with 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 来创建显示字符串.格式化程序的时区应该设置为您的,这是默认设置.

You should always use an NSDateFormatter to create a string for display. The formatter's timezone should be set to yours, which is the default.

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

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