如何在 Swift 语言中将公历日期选择器转换为波斯日期选择器?
func datePickerValueChanged(sender: UIDatePicker) {
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.NoStyle
dateFormatter.dateFormat = NSCalendarIdentifierPersian
dateOutlet.text = dateFormatter.stringFromDate(sender.date)
}
我将日期格式更改为NSCalendarIdentifierPersian
但日期选择器没有任何变化?
最佳答案
您需要设置日历
,而不是dateFormat
:
dateFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierPersian)
如果您希望更改语言,您还必须设置 locale
属性:
dateFormatter.locale = NSLocale(localeIdentifier: "fa_IR")
此外,正如问题注释中的评论,您在代码中使用的是 NSDateFormatter
,而不是 UIDatePicker
。幸好答案还是正确的; UIDatePicker
还有一个 calendar
属性,您可以设置该属性来实现此目标。
关于ios - 快速将公历日期选择器转换为波斯日期选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33372825/