日期格式化程序为6月返回nil [英] Date formatter returns nil for June
本文介绍了日期格式化程序为6月返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对这些代码行有一个奇怪的问题:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@MMyyyy];
NSDate * date = [df dateFromString:@062008];
NSLog(@Date%@,date);
结果是:
日期(null)
但是当我改变月份: / p>
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@MMyyyy];
NSDate * date = [df dateFromString:@072008];
NSLog(@Date%@,date);
结果是:
日期2008-06-30 23:00:00 +0000
只有六月(06),从字符串的日期转换失败!
我认为,这是与时区,任何想法的东西。
感谢
已解决:
[df setTimeZone:[NSTimeZone timeZoneWithName:@GMT]];
正如我想的,它与时区相关。
<$ c $ b c> [df setTimeZone:[NSTimeZone timeZoneWithName:@GMT]];
I have a strange problem with those lines of code :
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMyyyy"];
NSDate * date = [df dateFromString:@"062008"];
NSLog(@"Date %@", date);
The result is :
Date (null)
But when I change the month like this :
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MMyyyy"];
NSDate * date = [df dateFromString:@"072008"];
NSLog(@"Date %@", date);
The result is :
Date 2008-06-30 23:00:00 +0000
For only the month of June (06), the date conversion from string fails !
I think, it's something related to timezone, any ideas.
Thanks
solved :
[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
As I thought, it's related to timezone.
解决方案
solved : As I thought, it's related to timezone.
[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
这篇关于日期格式化程序为6月返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文