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?
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")
SwiftUI Sample :
DatePicker("", selection: self.$mySelectionDate, displayedComponents: .date)
.environment(\.locale, Locale.init(identifier: "fa_IR"))
.environment(\.calendar,Calendar(identifier: .persian))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With