Search code examples
swiftnsdateuidatepicker

Automatic select a date in datepicker in Swift language


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.


Solution

  • 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)
    }