如何使用NSDateFormatter将stringdate转换为NSDate

我有以下date作为NSString:

Thu May 29 14:22:40 UTC 2014 

我试着用下面的代码把它转换成NSDate:

 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @"EEE MMM d HH:mm:ss zzz yyyy"; NSDate *utc = [fmt dateFromString:@"Thu May 29 14:22:40 UTC 2014"]; NSLog(@"UTC Date:%@:", utc); 

结果是我试过几个dateFormat正则expression式,但没有运气。

我在这里错过了什么?

使用NSDataDetector类的NSRegularExpression的子类,它将采取一个未知date的string,并将其转换为NSDate对象,如果它发现匹配。

  NSError *error; NSDataDetector *data = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error]; NSString *dateRaw = @"Thu May 29 14:22:40 UTC 2014"; // your date NSDate *date = [data firstMatchInString:dateRaw options:NSMatchingReportCompletion range:NSMakeRange(0, dateRaw.length)].date; NSLog(@"Date: %@", date);