Rinto Andrews Rinto Andrews - 2 months ago 12
iOS Question

IOS Date Formatting ISSUE

i have date like this format

"lastUpdatedTime": "2016-09-03 02:01 AM",

I have to format like

03 Sep 2016 02:01 am


I have tried with
dateFormatter


Its not working

NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime];


but yourDate is nil

Answer

You need to use yyyy-MM-dd hh:mm a format for 12 Hour time formate you need to use hh instead of HH.

NSString *lastUpdatedTime = [JSONResultDict valueForKey:@"lastUpdatedTime"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd hh:mm a";
NSDate *yourDate = [dateFormatter dateFromString:lastUpdatedTime];
dateFormatter.dateFormat = @"dd MMM yyyy hh:mm a";
NSString *formttedDate = [dateFormatter stringFromDate: yourDate];
NSLog(@"%@",formttedDate);

Output

03 Sep 2016 02:01 AM
Comments