相同字符串格式的 NSDateFormatter 结果不一致
为什么这种转换有时有效,有时却无效? 当它不起作用时,eDate 为空。原始转换失败..但工作和转换之间的格式非工作日期字符串看起来相同。 我尝试在两个格式化程序中为 dd 而不是 d 添加额外的“d”。这并没有解决问题
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:@"EEE MMM d hh:mm:ss Z yyyy"];
NSDate *eDate = [dateFormatter dateFromString:[responceDictionary objectForKey: @"created_at"]];
NSLog(@"original string: %@", [responceDictionary objectForKey: @"created_at"]);
NSDateFormatter *shortFormatter = [[[NSDateFormatter alloc] init] autorelease];
[shortFormatter setDateFormat:@"EEE MMM d hh:mm"];
NSString *todayString = [shortFormatter stringFromDate:eDate];
NSLog(@"todayString: %@", todayString);
2011-05-12 11:01:52.768 Application[1707:207] original string: Wed May 11 19:30:57 +0000 2011
2011-05-12 11:01:52.769 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.770 Application[1707:207] original string: Tue May 10 18:45:01 +0000 2011
2011-05-12 11:01:52.770 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.771 Application[1707:207] original string: Tue May 10 02:24:48 +0000 2011
2011-05-12 11:01:52.771 Application[1707:207] todayString: Mon May 9 09:24
2011-05-12 11:01:52.772 Application[1707:207] original string: Tue May 10 01:05:05 +0000 2011
2011-05-12 11:01:52.772 Application[1707:207] todayString: Mon May 9 08:05
2011-05-12 11:01:52.773 Application[1707:207] original string: Mon May 09 17:38:14 +0000 2011
2011-05-12 11:01:52.774 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.774 Application[1707:207] original string: Fri May 06 12:36:30 +0000 2011
2011-05-12 11:01:52.775 Application[1707:207] todayString: Thu May 5 07:36
2011-05-12 11:01:52.775 Application[1707:207] original string: Fri May 06 12:08:58 +0000 2011
2011-05-12 11:01:52.776 Application[1707:207] todayString: Thu May 5 07:08
2011-05-12 11:01:52.777 Application[1707:207] original string: Fri May 06 11:56:44 +0000 2011
2011-05-12 11:01:52.778 Application[1707:207] todayString: Fri May 6 06:56
2011-05-12 11:01:52.778 Application[1707:207] original string: Thu May 05 22:17:48 +0000 2011
2011-05-12 11:01:52.779 Application[1707:207] todayString: (null)
Why does this conversion work some of the time and not others?
When it doesn't work, eDate is null. The original conversion fails.. yet the formats between a working & a non-working date string look identical.
I tried adding an extra "d" in both formatters for dd instead of d. That didn't fix the issue
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:@"EEE MMM d hh:mm:ss Z yyyy"];
NSDate *eDate = [dateFormatter dateFromString:[responceDictionary objectForKey: @"created_at"]];
NSLog(@"original string: %@", [responceDictionary objectForKey: @"created_at"]);
NSDateFormatter *shortFormatter = [[[NSDateFormatter alloc] init] autorelease];
[shortFormatter setDateFormat:@"EEE MMM d hh:mm"];
NSString *todayString = [shortFormatter stringFromDate:eDate];
NSLog(@"todayString: %@", todayString);
2011-05-12 11:01:52.768 Application[1707:207] original string: Wed May 11 19:30:57 +0000 2011
2011-05-12 11:01:52.769 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.770 Application[1707:207] original string: Tue May 10 18:45:01 +0000 2011
2011-05-12 11:01:52.770 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.771 Application[1707:207] original string: Tue May 10 02:24:48 +0000 2011
2011-05-12 11:01:52.771 Application[1707:207] todayString: Mon May 9 09:24
2011-05-12 11:01:52.772 Application[1707:207] original string: Tue May 10 01:05:05 +0000 2011
2011-05-12 11:01:52.772 Application[1707:207] todayString: Mon May 9 08:05
2011-05-12 11:01:52.773 Application[1707:207] original string: Mon May 09 17:38:14 +0000 2011
2011-05-12 11:01:52.774 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.774 Application[1707:207] original string: Fri May 06 12:36:30 +0000 2011
2011-05-12 11:01:52.775 Application[1707:207] todayString: Thu May 5 07:36
2011-05-12 11:01:52.775 Application[1707:207] original string: Fri May 06 12:08:58 +0000 2011
2011-05-12 11:01:52.776 Application[1707:207] todayString: Thu May 5 07:08
2011-05-12 11:01:52.777 Application[1707:207] original string: Fri May 06 11:56:44 +0000 2011
2011-05-12 11:01:52.778 Application[1707:207] todayString: Fri May 6 06:56
2011-05-12 11:01:52.778 Application[1707:207] original string: Thu May 05 22:17:48 +0000 2011
2011-05-12 11:01:52.779 Application[1707:207] todayString: (null)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在日期格式中使用
HH
而不是hh
。unicode 日期格式规范 规定
h
表示 12 (1-12) 小时格式的小时,H
表示 24 (0-23) 小时格式的小时。NSDateFormatter 无法创建日期,因为 22 (如上一个示例)超出了 12 小时的限制。
try
HH
instead ofhh
in your dateformat.The unicode date format specification states that
h
means hour in 12 (1-12) hour format andH
is the hour in 24 (0-23) hour format.The NSDateFormatter can't create a date because 22 (as in your last example) is beyond the limit of 12 hours.