iOS15 UIDatePicker shows strange year

Xcode13 and iOS15

Chinese calendar UIDatepicker the year of 2021 will become 38, how to change it back to 2021 in UIDatepicker

datePicker.datePickerMode = .date datePicker.locale = Locale.init(identifier: "zh") datePicker.calendar = Calendar(identifier: .chinese)

I tested your code in an app.

I get a correct 2021 date as shown below:

But you're right, when I select a date, I get a different year (and month)

I did the same test with Xcode 12 / iOS 14.4 and got the same problem. So it is not an iOS 15 bug.

I removed

datePicker.calendar = Calendar(identifier: .chinese)

And it worked well.

It is not a bug. The reason is that chinese calendar is sexagenary based. If you don't specify era, you get a value between 1 and 60 ! So, setting calendar to .chinese make you display dates (as at top left of the view) in the current era.

Read this: https://stackoverflow.com/questions/60936744/how-to-convert-gregorian-dates-to-chinese-not-sexagenary-cycle-in-swift

"What is the Month and Day of Chinese New Year for a Gregorian year?". In my journey I learned that the Chinese calendar follows the sexagenary cycle of 60year eras. Era is an important concept because you need to use it to get to the right Gregorian year. Not specifying the an Era on the Chinese calendar will return a Month and Day in the current Era which began in 1984.


let datePicker = UIDatePicker()
datePicker.locale = .current
datePicker.datePickerMode = .date
datePicker.preferredDatePickerStyle = .wheels
datePicker.locale = Locale(identifier: "zh")
datePicker.calendar = Calendar(identifier: .chinese)

The same code I got different result in iOS15 and iOS 14.2. The year become"38" but not "2021" in iOS 15

iOS15 UIDatePicker shows strange year
 
 
Q