dateFromString返回null 这是我的代码:
NSString *dateString = @"Wed 16 Oct 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"EEE dd MMM yyyy"];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
NSDate *myDate = [dateFormatter dateFromString: dateString];
请给我任何有关此事的建议。
答案 0 :(得分:1)
这是一个区域设置问题,它与NSDateFormatter Strings in iOS 7相同,只是相反。
可能在您的系统区域设置中Wed
或/和Oct
对于工作日或/和月份无效。
尝试将区域设置设置为en_US
或currentLocale
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
或
NSLocale *locale = [NSLocale currentLocale];
答案 1 :(得分:0)
了解更多格式样式http://cybersam.com/ios-dev/quick-guide-to-ios-dateformatting
NSString *dateString = @"Wed 16 Oct 2013";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"EEE dd MMM yyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"%@", dateFromString);