change date format in iPhone
ios, iphone, nsdateformatter
Solution
The most important part of date formatting is often forgotten, tell the `NSDateFormatter` the 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've checked the output: `date: 2012-05-14 10:00:55 +0000`
Be aware that the `HH` in the date formatter is for `24hr`.
Now I would suggest not to use a fixed output scheme, but use one of the `NSDateFormatterStyle`:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"date: %@", dateString);
Which in american english will output: `5/14/12 12:00 PM`
This code is valid for `ARC` if you are not using `ARC` add `autorelease` to the `NSLocale` and release the `NSDateFormatter` after you are done with it.
Problem
I am trying this since last two hours and finaly left it out for you guys :P I need to convert this String `Mon, 14 May 2012, 12:00:55 +0200` into Date `dd/mm/yyyy hh:ss` format.. Any kind of help towards the goal will be really appreciated. What I have tried I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date. This `[dateFormatter setDateFormat:@"EEEddMM,yyyy HH:mm:ss"];` is how I have tried and many other formats too Eg: ``` NSString *finalDate = @"Tue, 29 May 2012, 14:24:56 +0200"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEEddMM,yyyy HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:finalDate]; ``` Here the date alwasys comes as nil