是否可以为日期设置多个“setDateStyle”?像“NSDateFormatterShortStyle,NSDateFormatterMediumStyle”因为我需要输入两种类型的日期并且需要接受两种类型。可能吗?
最佳答案
您可以尝试将 NSDataDetector
与 NSTextCheckingTypeDate
一起使用,它可以识别许多日期格式。
NSString *dateText = @"march 31, 1945 12:34 pm";
NSError *error;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSTextCheckingResult *result = [detector firstMatchInString:dateText options:0 range:NSMakeRange(0, dateText.length)];
NSDate *date = result.date;
NSString *reformattedDate = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterFullStyle];
NSLog(@"reformattedDate: %@", reformattedDate);
NSLog 输出:
reformattedDate date: Saturday, March 31, 1945 at 12:34:00 PM
关于ios - 是否可以为日期设置多个 "setDateStyle"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23934115/