Swift Format Date

孤街醉人 提交于 2019-12-25 04:44:12

问题


This is probably really simple, I have tried lots of different methods but it doesnt seem to work.

I have the following date format in a text field:

Saturday, 2 January 2016 12:00

held as:

  var str = "\(dobTextField.text!)"

I would like to convert the string above into the format:

YYYY-mm-dd

any help would be much appreciated!


回答1:


Check this :

let str = "Saturday, 2 January 2016 12:00"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, d MMMM yyyy HH:mm" 
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let outputA = dateFormatter.dateFromString(str)
dateFormatter.dateFormat = "yyyy-MM-dd"
println(dateFormatter.stringFromDate(outputA!))

Output :

2016-01-02



来源:https://stackoverflow.com/questions/31316207/swift-format-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!