The most important part of date formatting is often forgotten; specify the NSDateFormatter
input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"EEE, dd MMMM yyyy, HH:mm:ss Z"]; dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"EN"]; NSDate *date = [dateFormatter dateFromString:@"Mon, 14 May 2012, 12:00:55 +0200"]; NSLog(@"date: %@", date);
I checked the output: date: 2012-05-14 10:00:55 +0000
Keep in mind that HH
in date formatting for 24hr
.
Now, I would suggest not using a fixed output scheme, but using one of NSDateFormatterStyle
:
[dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *dateString = [dateFormatter stringFromDate:date]; NSLog(@"date: %@", dateString);
which in American English displays: 5/14/12 12:00 PM
This code is valid for ARC
unless you use ARC
add autorelease
in NSLocale
and release NSDateFormatter
after you are done with it.
source share