是否可以为日期设置多个“setDateStyle”?

时间:2014-05-29 13:07:58

标签: ios nsdate

是否可以设置多个" setDateStyle"约会?喜欢" NSDateFormatterShortStyle,NSDateFormatterMediumStyle"因为我需要输入两种类型的日期,需要同时接受这两种日期。有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试将NSDataDetectorNSTextCheckingTypeDate一起使用,以识别许多日期格式。

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:1945年3月31日星期六下午12:34:00

相关问题