Convert gregorian datepicker to persian datepicker in swift?
You need to set the calendar
, not the dateFormat
:
dateFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierPersian)
You would also have to set the locale
property if you want the language to change:
dateFormatter.locale = NSLocale(localeIdentifier: "fa_IR")
Also, as the comment on the question notes, you're working with an NSDateFormatter
, not a UIDatePicker
in your code. Fortunately the answer is still correct; UIDatePicker
also has a calendar
property that you would set to accomplish this goal.
Swift 3:
As Charles A. Said DatePicker
has a calendar property and you can use it to have persian datepicker or even :
datePicker.calendar = Calendar(identifier: .persian)
datePicker.locale = Locale(identifier: "fa_IR")