小编Vin*_*Xie的帖子

NSDateFormatter无法将"1988-04-10"解析为iOS 6中的NSDate实例.这是一个错误吗?

我遇到了一个陌生人的问题.NSDateFormatter无法解析1988-04-10到iOS 6中的NSDate实例.但它适用于iOS 4和iOS 5.这是一个错误吗?

以下是代码.这很简单.

NSArray *values = @[@"1988-04-09", @"1988-04-10", @"1988-04-11"];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[df setLocale:posixLocale];
[df setDateFormat:@"yyyy'-'MM'-'dd"];
NSLog(@"timeZone:%@, locale:%@", df.timeZone, df.locale.localeIdentifier);
NSDate* date = nil;
for (NSString *value in values) {
    date = [df dateFromString:value];
    NSLog(@"The date is %@", date);
}
[posixLocale release];
[df release];
Run Code Online (Sandbox Code Playgroud)

以下是iOS 4和iOS 5中的结果.

2013-03-19 09:55:32.249 SampleProject[1844:f803] timeZone:Asia/Shanghai (CST (China)) offset 28800, locale:en_US_POSIX
2013-03-19 09:55:32.250 SampleProject[1844:f803] The date is 1988-04-08 16:00:00 +0000
2013-03-19 09:55:32.250 …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c nsdate nscalendar ios

3
推荐指数
1
解决办法
328
查看次数

标签 统计

cocoa-touch ×1

ios ×1

nscalendar ×1

nsdate ×1

objective-c ×1