简体   繁体   中英

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?

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"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM