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

主宰稳场 提交于 2019-12-01 15:09:49

问题


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:


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



来源:https://stackoverflow.com/questions/5878528/is-there-a-way-to-convert-a-natural-language-date-nsstring-to-an-nsdate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!