Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert a natural language date NSString to an NSDate

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.

like image 595
Joel Avatar asked May 04 '11 04:05

Joel


1 Answers

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.”

like image 158
alfred Avatar answered Oct 14 '22 08:10

alfred