Search code examples
iosswiftnsdateformatter

datepicker format now show month perfectly in ios swift 4.1?


@objc func todatePickerDone(){
    let formatter = DateFormatter()
    formatter.dateFormat = "dd/mm/yyyy"
    toDateText.text = formatter.string(from: toDatePicker.date)
    self.view.endEditing(true)

}

The result show like this.

Coming todate here 23/49/2018

Coming fromDate here 04/51/2017


Solution

  • You have to check the format of Date Component first.

    check this website and compare with your date format.

    In your example you had used mm which represent minute component.

    and for month you need to MM.

    See below other format for month.

    1) For short text month i.e Jun - MMM

    2) for full Month name i.e June - MMMM

    You solution

    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    toDateText.text = formatter.string(from: toDatePicker.date)
    self.view.endEditing(true)