donparalias donparalias - 1 year ago 87
iOS Question

Changing date format on iOS

I have some dates in this format :

2012-06-26 12:00:00 +0000

and what i want to do is convert them in this format :

Jun 26, 2012 12:00:00 AM

Is there an easy way to do this on obj-c , or i must parse everything hardcore , and make my own function for this? If yes , any ideas how this would look like , cause i am new on iOS.
Thank you very much for reading my post :D

Answer Source

To convert from 1 string to the other use the following

NSString *inputString = @"2012-06-26 12:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss z"];//input

NSDate *date = [formatter dateFromString:inputString];

[formatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(@"output : %@",outputString);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download