views:

455

answers:

2

I have an NSString that might contain 5-10 different style dates and times. I need to convert all of them into NSDates.

Does dateWithNaturalLanguageString no longer exist in iPhone SDK 3.1.3?

I do NOT need to convert things like @"last Wednesday at 1 pm".

I need to convert things like: @"01-Feb-2010" @"Feb 1, 2010" @"February 1, 2010" @"12/31/2010" @"12-31-2010"

(Every example I've seen requires that you already KNOW the format of the NSString. I have NO idea what format it will be.)

+2  A: 

Do you know all of the possible formats? You could always try creating an NSDateFormatter for each one, then trying a string until a valid date is returned. That's pretty kludgy, but might be better than nothing.

Jeff Kelley
I don't know the date format. But it would be a basic, simple, commonly used one. In other languages it is just 1 simple function call: date = CDate(string). Done. Not sure why Apple/C/Objective-C puts it somewhere between "very hard to do" and "impossible to do".
Bonnie
A: 

I don't see dateWithNaturalLanguageString in any iPhone SDK version; it was probably an error in an older version of the documentation (the equivalent functionality in Mac OS X was deprecated in 10.4 and never worked very well anyway).

Instead, just try parsing with each of the various date formats in turn.

Implementing natural-language date parsing, particularly in a way that works for every locale, is very hard. If you need natural-language date parsing in another context, the best I've found is the Date::Manip Perl module.

Nicholas Riley