NSDateFormatter在iOS 5中返回nil

时间:2012-07-10 12:44:22

标签: iphone ios xcode ios5 nsdateformatter

使用iOS 6一切正常,但iOS 5 myDate返回nil

NSString *dateString = @"2012-07-12T11:00:00+02:00";

NSLocale *deLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZZ"];
dateFormatter.locale = deLocale;
NSDate *myDate = [dateFormatter dateFromString:dateString];

我相信它一定是DateFormat的问题

2 个答案:

答案 0 :(得分:1)

NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateStyle:NSDateFormatterShortStyle];
 NSString *dateString = [dateFormat stringFromDate:today];
 NSLog(@"Date: %@", dateString);

答案 1 :(得分:-1)

这对我有用:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]);