【发布时间】:2014-08-06 01:33:21
【问题描述】:
我正在迭代一个 for 循环并从 json 字符串中获取我的日期作为字符串。发生的事情是,我正在尝试将每个日期字符串转换为 NSDate。我遇到的问题是,每次日期以 08 以外的日期开始时,即 09-13-2014,我都会不断返回 null。我已经更改了服务器上的日期并测试了日期,只要日期以 08 开头,即 08-31-2014,它就可以完美运行。有什么想法吗?
NSString *myDate = item[@"stringdate"];
NSString *yourDate = item[@"sswStringDate"];
NSLog(@"thedate %@", myDate);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:myDate];
[dateFormatter setDateFormat:@"MM-dd-YYYY"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
NSLog(@"sst %@", stringFromDate);
我的日志语句是
thedate 08-31-2014
sst 08-31-2014
thedate 08-20-2015
sst 08-20-2014
thedate 11-31-2014
sst (null)
thedate 09-31-2014
sst (null)
thedate 08-31-2014
sst 08-31-2014
【问题讨论】:
-
上次查了,11月和9月只有30天。
-
30 天有 9 月、4 月、6 月和 11 月,其余的都有 31 天,除了 2 月,因为它很笨。
-
天啊!!!我怎么看不到!!!谢谢!!!
标签: ios objective-c nsdate nsdateformatter