naohide_a
naohide_a

Reputation: 1146

how can I convert this date format in swift?

I want to convert Wed Jul 01 04:48:51 +0000 2015 to 2015-07-01
I tried below but it could't execute well(returned nil).

let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date: NSDate? = formatter.dateFromString(d)

Is it possible to convert this date format?

Upvotes: 0

Views: 306

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236568

let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
if let date = formatter.dateFromString(d) {
    formatter.dateFormat = "yyyy-MM-dd"
    let dateString = formatter.stringFromDate(date)
    println(dateString)   // "2015-07-01"
}

Upvotes: 2

Related Questions