UIDatePicker返回错误的NSDate [英] UIDatePicker return wrong NSDate

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

问题描述

我在我的应用程序中使用UIDatePicker,当我选择使用的日期时:

I am using UIDatePicker in my app and when i take the date that was chosen with:

NSDate *date = picker.date;

picker.date 在前一天返回我选择的日期。

picker.date returned the day before the date that I chose.

知道为什么会这样吗?

推荐答案

UIDatePicker 将在您当地的时区显示日期和时间。但是, NSDate 没有任何时区概念,因为它存储自参考日期以来的绝对秒数。当 NSLogging 日期时,它以GMT显示日期和时间。我希望如果你从GMT计算出你当地的时区差异,你会发现它是正确的日期。

UIDatePicker will be displaying dates and times in your local timezone. However, NSDate does not have any concept of a timezone as it stores an absolute number of seconds since a reference date. When NSLogging a date, it shows the date and time in GMT. I expect if you work out your local timezone difference from GMT, you will see that it is the correct date.

尝试使用适当的语言环境创建NSDateFormatter或NSCalendar并传递通过该日期。

Try creating an NSDateFormatter or NSCalendar with the appropriate locale and pass the date through that.

有关此常见主题的进一步阅读,请参阅此网站由另一个SO贡献者撰写

For further reading on this common topic, see this site written by another SO contributor.

这篇关于UIDatePicker返回错误的NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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