Jigen Jigen - 1 year ago 359
Swift Question

Convert date string swift

I have a date string in this format:


and I need to change it to something like this:


Previously I have always used this library -> SwiftDate to manipulate the dates, but it doesn't work now.

I tried also something like:

let myDate = self.dateNoteDict[indexPath.row]!
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
let date = dateFormatter.dateFromString(myDate)
print("date -> \(date)")

but it doesn't work. How can I do?

Thanks in advance.

Answer Source

The standard ISO8601 date format with fractional seconds and time zone is


let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.dateFromString(myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.stringFromDate(date)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download