NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
NSString *dateString = @"Tue, 08 Jun 2010 17:00:00 EDT";
NSDate *eventDate = [dateFormatter dateFromString:dateString];
In this case the eventDate object is nil. Can somebody clue me in? This code used to work.
UPDATE: Can't talk about why this doesn't work due to NDA. Suffice it to say, when iOS 4 is out I will post the answer to my own question.
The full list of format specifiers is UTS#35 Date Format Patterns.
Does the 'c' character work in place of 'E'? The document has it as a very close alternative and it may produce the result you want.
(If you really want characters in the format string that are not in the table you can escape them, like
hh 'o''clock' a, zzzz
- produces format like "12 o'clock PM, Pacific Daylight Time".)The problem that I found is that the string that I was parsing has some trailing characters "\n\t\t". The solution was to remove them:
[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
The answer to this question is the following: I was using the wrong date format string:
when it should have been:
The part about iOS 4 and NDA was that I thought I had to use the
NSDateFormatter
methoddateFormatFromTemplate:options:locale:
which would have looked like this:However, that method should only be used when you want to DISPLAY the date to a user of unknown locale. In my case, I knew exactly what the date format was going to look like and I was trying to PARSE the date string so that I could store it in CoreData. Therefore, that method wasn't useful.
Bonus bookmark: Read this table very carefully and you will definitely figure out what the problem is... Unicode date formats should follow these specifications: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table
TL;DR The format string was wrong. D'oh!
This is my comment for date parsing. I use the following, where
toDateUsingFormat
uses anNSDateFormatter
with the passed in string. I do not use a locale, because rss dates are not localized.Edit:
I use
getObjectValue:
instead ofdateFromString
.you have a zero padded day, namely
08
in your date string, however in your format string the format is trying to parse a non-zero padded day, namelyd
. changingd
todd
should fix the problem