NSDate date don't give me the right hour

谁说胖子不能爱 提交于 2019-11-28 13:01:55

问题


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.

Log http://data.imagup.com/12/1171458807.5637png

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


回答1:


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];

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



来源:https://stackoverflow.com/questions/14082912/nsdate-date-dont-give-me-the-right-hour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!