Is there a way to convert a natural language date

2019-02-21 21:27发布

Say I have the NSString @"tomorrow"

Is there any library that takes strings such as this and converts them into NSDates? I'm imagining/hoping for something like this:

NSString* humanDate = @"tomorrow at 4:15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"x at HH:MM"];
NSDate* date = [dateFormatter dateFromString:humanDate];

I would also want to do things like "Next monday", etc. but it doesn't have to be super sophisticated. I can enforce rules on input, but I'd like a little bit of natural language available.

My alternative is to take the string, break it up into pieces, and format it manually. But I was hoping someone has already done this.

1条回答
我想做一个坏孩纸
2楼-- · 2019-02-21 22:15

Do you mean something like dateWithNaturalLanguageString:?

From the link:

dateWithNaturalLanguageString:

Creates and returns an NSDate object set to the date and time specified by a given string.

+ (id)dateWithNaturalLanguageString:(NSString *)string

A string that contains a colloquial specification of a date, such as “last Tuesday at dinner,” “3pm December 31, 2001,” “12/31/01,” or “31/12/01.”

查看更多
登录 后发表回答