NSString to NSDate conversion getting the wrong result [duplicate]

偶尔善良 提交于 2019-12-04 07:33:07

问题


Possible Duplicate:
Wrong time from NSDateFormatter
NSDate is 5 hours off

I am trying to convert NSString to NSDate with the code

result = @"2012-02-09";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:result];
[dateFormatter release];
NSLog(@"date: %@", dateFromString);

But the date conversion giving an incorrect result, in logs:

2012-02-07 13:08:29.553 Document[611:15503] date: 2012-02-08 19:00:00 +0000

Can someone please tell me whats wrong with my code?


回答1:


To use NSLog to display a date you should use your formatter so that the NSDate will be formatted with your TimeZone:

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


来源:https://stackoverflow.com/questions/9172960/nsstring-to-nsdate-conversion-getting-the-wrong-result

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