I have a string 20160522115200
and need to convert into yyyy-mm-dd hh:mm:ss
format.
Ex: 2016-05-22 11:52:00 yyyy-mm-dd hh:mm:ss
I have a string 20160522115200
and need to convert into yyyy-mm-dd hh:mm:ss
format.
Ex: 2016-05-22 11:52:00 yyyy-mm-dd hh:mm:ss
Try below line of code:
NSString * str = @"20160522115200";
NSDateFormatter * d1 = [[NSDateFormatter alloc] init];
d1.dateFormat = @"yyyyMMddhhmmss";
NSDate * date = [d1 dateFromString: str];
d1.dateFormat = @"yyyy-MM-dd hh:mm:ss";
str = [d1 stringFromDate: date];
In Swift You will need to do the following:
Format the current Date String :
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyyMMddhhmmss"
let date = formatter.dateFromString("20160522115200")
Convert the new String to your new format :
let formatterToNewDate = NSDateFormatter()
formatterToNewDate.dateFormat = "yyyy-MM-dd hh:mm:ss"
let myNewDate = formatterToNewDate.stringFromDate(date!)