我将以下日期作为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 regex表达式,但是没有运气。
我在这里想念什么?
使用NSDataDetector类是NSRegularExpression的子类,它使用日期未知的字符串,如果找到匹配项,则将其转换为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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句