问题
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