有时在NSDate中获得一小时的差异 [英] Sometimes Getting difference of an hour in NSDate

查看:165
本文介绍了有时在NSDate中获得一小时的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IOS 6和IOS 5.我在一小时的时间里有所不同。虽然我使用日期格式为 @MMM d,yyyy HH:mm aaa。我也试过 @MMM d,yyyy h:mm aaa

I am using IOS 6 and IOS 5. I am getting difference in time of an hour. while I have used the date format as @"MMM d, yyyy HH:mm aaa". I have also tried @"MMM d, yyyy h:mm aaa".

实际上,我正在设置当前本地通知的固定时间。对于不同的日期,时间固定为上午10:00。它在控制台中正确显示日期,但时间因澳大利亚时区而异,显示2012-10-02 14:00:00 +0000或2012-10-02 13:00:00 0000\"

Actually, I am setting fixed time of notifications for current local. The time is fixed as 10:00AM for different dates. It is displaying date correctly in console but in time is varying as for Australia Time Zone it is displaying "2012-10-02 14:00:00 +0000 or 2012-10-02 13:00:00 +0000".

当我将日期格式更改为 @MMM d,yyyy h:mm aaa时显示2012-10-02 00:00:00 +0000或2012-10-02 23:00:00 +0000

When I have change the date format as @"MMM d, yyyy h:mm aaa" it is displaying "2012-10-02 00:00:00 +0000 or 2012-10-02 23:00:00 +0000".

请建议。

推荐答案

使用日期格式化程序时,你应该指定日历和时区,以正确的格式返回日期。

When using a date formatter, you should specify the calendar and time zone to return dates in the correct format.

我发现添加以下两行有助于缓解此问题。 (你看我在那里做了什么?)

I've found that adding the following two lines has helped alleviate this issue in the past. (You see what I did there?)

[formatter setCalendar:[NSCalendar currentCalendar]];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

这篇关于有时在NSDate中获得一小时的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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