how can I convert this date format in swift?

后端 未结 1 1040
予麋鹿
予麋鹿 2021-01-28 12:37

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 = \"         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-28 13:43

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

    0 讨论(0)
提交回复
热议问题