相同字符串格式的 NSDateFormatter 结果不一致

发布于 2024-11-07 00:37:23 字数 2232 浏览 0 评论 0原文

为什么这种转换有时有效,有时却无效? 当它不起作用时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-11-14 00:37:23

尝试在日期格式中使用 HH 而不是 hh

unicode 日期格式规范 规定 h表示 12 (1-12) 小时格式的小时,H 表示 24 (0-23) 小时格式的小时。

NSDateFormatter 无法创建日期,因为 22 (如上一个示例)超出了 12 小时的限制。

try HH instead of hh in your dateformat.

The unicode date format specification states that h means hour in 12 (1-12) hour format and H 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文