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: % * ?
This is apparently not possible with the NSDateFormatter.
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.
精彩评论