我正在尝试使用包含日期/时间的 NSString,并从中创建一个 NSDate 对象,以便我可以进行相应的操作。
我过去使用过 NSDataDetector 没有问题。但是,当我现在尝试时,detectedDate 正在返回当前日期,而不是在字符串中找到的日期。
有人可以解释为什么这不会像我过去使用的那样从字符串中返回日期吗?或建议如何最好地实现这一目标?
非常感谢。
NSString *myDate = @"2014-03-13T12:31:00-07:00";
__block NSDate *detectedDate;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
[detector enumerateMatchesInString:myDate
options:kNilOptions
range:NSMakeRange(0, [myDate length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{ detectedDate = result.date; } ];
NSLog(@"Result:%@",detectedDate);