views:

47

answers:

2

How can I parse a string that represents a date and/or time using iPhone SDK if I do not know the format? I am building an application that will allow a range of possible dates and times to be used, and I don't want it to fail because the format was "01/01/2001" instead of "01-01-2001", etc. Time may or may not be included.

Is there any way to do this? I have tried using NSDateFormatter with date and time styles set to "NoStyle", and setLenient = YES, but it still fails to parse the simplest of strings: "1/22/2009 12:00:00 PM"

I have no way of knowing the format in advance, I need a method that will heuristically determine the format.

I am coming from a .NET background, where this is as simple as new DateTime(string_in_any_format); but I have no idea how to do this in Obj-C.

+1  A: 

Unfortunately, NSDateFormatter on the iPhone isn't that smart. You need to give it a little guidance. When you set both the date and time styles to NoStyle it expects to see neither a date nor time. You'll need at least one of them set to some other style. You can do something like the following and it should work fine (it does for me).

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLenient:YES];

NSDate * date = [formatter dateFromString:@"01-01/2001"];

NSLog(@"%@", date);
Cory Kilger
Thank you for your help. I could see that this would be a helpful thing to build, but since it doesn't seem to be provided, I will just build my own. For the time being I will just support limited formats.
Dan Berlin
A: 

How would your potential parser determine if 01/02/10 is jan 2, 2010; feb 1, 2010; or something entirely different? Bboth mm/dd/yy and dd/mm/yy are reasonable to expect, depending on where your customers are.

erikkallen
The parser can assume one format or the other based on locale, so it is acceptable to make that one assumption. This is the only acceptable assumption, however.
Dan Berlin
So basically what you're asking for is how to make the parser not make any difference between - and / ?
erikkallen