Automatic select a date in datepicker in swift lan

2019-05-23 09:16发布

问题:

I have a date "Thursday, 18 Sep 2014" in this format. when i click a "change date" button. how we can automatic select a this date in datepicker in swift language.

回答1:

var dateString = "Thursday, 18 Sep 2014"
var df = NSDateFormatter()
df.dateFormat = "eeee, dd MM yyyy"
var date = df.dateFromString(dateString)
if let unwrappedDate = date {
    datePicker.setDate(unwrappedDate, animated: false)
}