为什么这种转换在某些时候有效而在其他时间无效? 如果它不起作用,则eDate为null。原始转换失败..但是工作和放大之间的格式非工作日期字符串看起来相同。 我尝试在两个格式化程序中为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)
答案 0 :(得分:4)
在日期格式中尝试HH
而不是hh
。
unicode date format specification表示h
表示12(1-12)小时格式的小时,H
表示24(0-23)小时格式的小时。
NSDateFormatter无法创建日期,因为22(如上一个示例中所示)超出了12小时的限制。