Search code examples
iosswiftuidatepicker

Convert gregorian datepicker to persian datepicker in swift?


How can I convert Gregorian datepicker to persian datepicker in Swift language ?

func datePickerValueChanged(sender: UIDatePicker) {

    let dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle

    dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
    dateFormatter.dateFormat = NSCalendarIdentifierPersian

    dateOutlet.text = dateFormatter.stringFromDate(sender.date)

}

I changed date format toNSCalendarIdentifierPersian but there isnt any change in datepicker?


Solution

  • 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.