Reputation: 2816
How can I convert Sep 15, 2015, 20:49:00
to 2015-09-15T20:49:00
using Swift? I'm fetching a date
object from Parse but need to convert it to a different format.
EDIT AFTER BEING MARKED DUPLICATE:
I'm using this :
let time = "Sat Sep 19 2015 19:03:47 GMT+0000 (UTC)"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS'Z'"
let date = dateFormatter.dateFromString(listenTime)
...and getting nil
. So, what am I doing wrong?
Upvotes: 0
Views: 111
Reputation: 236568
let time = "Sat Sep 19 2015 19:03:47 GMT+0000 (UTC)"
let df = NSDateFormatter()
df.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
df.locale = NSLocale(localeIdentifier: "en_US_POSIX")
df.timeZone = NSTimeZone(abbreviation: "GMT")
df.dateFormat = "EEE LLL dd yyyy HH:mm:ss' GMT+0000 (UTC)'"
if let date = df.dateFromString(time) {
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let stringFromDate = df.stringFromDate(date) // "2015-09-19T19:03:47"
}
Upvotes: 1