NSString to NSDate conversion getting the wrong re

2019-03-07 01:19发布

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条回答
对你真心纯属浪费
2楼-- · 2019-03-07 01:48

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]);
查看更多
登录 后发表回答