NSString到NSDate转换得到错误的结果 [英] NSString to NSDate conversion getting the wrong result
本文介绍了NSString到NSDate转换得到错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图用代码
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?
推荐答案
要使用NSLog显示日期,您应该使用格式化程序,以便使用TimeZone格式化NSDate:
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]);
这篇关于NSString到NSDate转换得到错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文