I'm trying to format date in date picker. I can't seem to find the format to display date in 09-Nov-2014 format. It always displays in Nov 09, 2014 format. I did a lot of search but can't figure out which one is the correct format to input. Can anyone help me out? Tx in advance.
@objc func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.dateStyle = DateFormatter.Style.medium
dateFormatter.timeStyle = DateFormatter.Style.none
dateOfBirthTextField.text = dateFormatter.string(from: sender.date)
}
Don't set the styles. Just set the format.
You use either dateFormat
or you use dateStyle
and timeStyle
.
Please note it's best to avoid using dateFormat
for dates you wish to show to the user since your hardcoded format won't be standard for most users.
Also be aware that MM
show the month number. If you want the abbreviated month name, use MMM
.