NSDate日期没有给我正确的时间 [英] NSDate date don't give me the right hour

查看:145
本文介绍了NSDate日期没有给我正确的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取当前日期:

I'm trying to get the current date with :

NSLog(@"DATE NOW : %@", [NSDate date]);

但是当我执行它时,我在显示的日期和当前日期之间有一小时的差异。

But when I execute it, I have a difference of one hour between the date displayed and the current date.

记录http://数据。 imagup.com/12/11/11758807.5637png

我找到了类似问题的主题,但没有一个给我答案。
如何更改?

I found topics on questions like that but none of them gave me the answer. How can I change that ?

推荐答案

您需要设置正确的时区。正如您所见,默认值为UTC。

You need to set the proper timezone. The default, as you can see, is UTC.

您可以使用日期格式化程序来执行此操作。

You can use a date formatter for doing that.

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone * timezone = [NSTimeZone timeZoneWithName:@"Europe/Rome"];
[dateFormatter setTimeZone:timezone];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle
 ];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];    


NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

有关可用时区的列表,您可以使用

For a list of available timezones you can use

NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];

或查看此Rails上的常量 doc page

or check out the constants here on this Rails doc page.

这篇关于NSDate日期没有给我正确的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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