我有以下日期作为 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);
结果是nil我已经尝试了几个日期格式正则表达式,但没有运气。
我在这里缺少什么?
Use NS数据检测器 https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSDataDetector_Class/Reference/Reference.html类 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)