Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 492
Mohammad Alikhani Avatar asked Oct 27 '15 16:10

Mohammad Alikhani


3 Answers

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.

like image 77
Charles A. Avatar answered Nov 18 '22 14:11

Charles A.


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")
like image 30
Milad Faridnia Avatar answered Nov 18 '22 15:11

Milad Faridnia


SwiftUI Sample :

 DatePicker("", selection: self.$mySelectionDate, displayedComponents: .date)
                        .environment(\.locale, Locale.init(identifier: "fa_IR"))
                        .environment(\.calendar,Calendar(identifier: .persian))
like image 3
Farshid roohi Avatar answered Nov 18 '22 14:11

Farshid roohi