If your starter string is May 2, 2013 6:46:33 PM
then you have two issues:
- Your format string
MM dd, yyyy hh:mm:ss a
does not match your string. It needs to beMMMM dd, yyyy hh:mm:ss a
. The use ofMM
is for numeric months. UseMMM
for abbreviated month names and useMMMM
for full month names. - Your date string has month names in English. If your device is setup for Italy then it won't properly parse the month names since it will expect the months names to be in Italian.
Your code should be:
NSString *dateStr = @"May 2, 2013 6:46:33 PM";
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:@"MMMM dd, yyyy hh:mm:ss a"];
[inputDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
NSDate *dateFromString = [inputDateFormatter dateFromString:dataStr];
NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSString *stringFromDate = [outputDateFormatter stringFromDate:dateFromString];