4
votes

I'm trying to use only a NSDateFormatter to format string representation of a date/time to an NSDate object. The issue is that I can't figure out how to allow the ordinal suffixes in the format.

So lets say I have a string "Wednesday, August 11th 2010 8:00 PM"

What one line NSDate dateFormat should I use?

Like "EEEE, MMM dd'th' yyyy h:mm a" would work, but that will only work for ordinal days ending in 'th', whereas i need a single format that will allow for 1st, 2nd, 3rd, 4th 5th etc.

It seems like a wildcard character in the format string to accomplish this. I've tried a few things like: % * ?

2
That's for the other way around. NSDate to NSString, i need NSString to NSDatepsy
Generally, NSDateFormatter gets confused if you try to parse a date containing a day of the week. Best to strip that off and just parse the remainder.Hot Licks

2 Answers

3
votes

This is apparently not possible with the NSDateFormatter.

-1
votes

You want to use an NSDateFormatter like so:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
[dateFormatter setDateStyle: NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];

Either NSDateFormatterLongStyle or NSDateFormatterFullStyle should get you the results you're looking for.