I write below code to convert one date format into another dateformat. But AM string works and PM did not work . Please help-
dateTime=@"2013-01-24 18:30 PM";
//dateTime=@"2013-01-24 06:30 AM";
NSDateFormatter *dtFormatter=[[NSDateFormatter alloc]init];
[dtFormatter setDateFormat:@"yyyy-MM-dd hh:mm a"];
NSDate *AptDate=[dtFormatter dateFromString:dateTime];
[dtFormatter setDateFormat:@"MMM dd,yyyy hh:mm a"];
NSString *strDate=[dtFormatter stringFromDate:AptDate];
Here I get AptDate variable as null if i use PM format
You need to use
HH
in place ofhh
, as your time is in 24 hour format:Your full code will go as:
Output:
For those who are looking for Swift 3 version of Accepted answer.
For anyone looking from mySQL with a DATETIME stamp to a regular hh:mm a format showing AM/PM correctly this worked for me.