简体   繁体   English

从Objective-C中的描述字符串获取日期

[英]Get date from description string in Objective-C

I would like to know if there is a way to find and convert a date from a string which contain some other informations like : 我想知道是否有一种方法可以从包含一些其他信息的字符串中查找和转换日期:

"Your driver will be here on Monday" or "Be present the November 24th" for example. 例如,“您的司机将在星期一到这里”“ 11月24日到场”

In order to create a NSDate from this information. 为了从此信息创建一个NSDate。

Thank you guys ! 感谢大伙们 !

Please try below function: 请尝试以下功能:

-(void)fetchDateFromString:(NSString*)stringObj
{

    NSError *error = NULL;
    NSDataDetector *detectorObj = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];

    NSArray *matchesObj = [detectorObj matchesInString:stringObj
                                         options:0
                                           range:NSMakeRange(0, [stringObj length])];

    NSLocale* currentLoc = [NSLocale currentLocale];
    for (NSTextCheckingResult *match in matchesObj) {
        if ([match resultType] == NSTextCheckingTypeDate) {
            NSLog(@"Date : %@", [[match date] descriptionWithLocale:currentLoc]);
        }
    }
}

And invoke as below: 并如下调用:

[self fetchDateFromString:@"Be present the November 24th"];

Good Luck..... 祝好运.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM