Home > database >  Unexpected year on creating date from japanese Calendar
Unexpected year on creating date from japanese Calendar

Time:12-20

let dateComponent = DateComponents(year: 2022, month: 12, day: 30)
print(Calendar(identifier: .japanese).date(from: dateComponent))

4040-12-29 17:00:00  0000

Is there any idea why the returned date's year is 4040? Thanks

I'm assuming there is a different year format, but I don't find any information about it.

CodePudding user response:

let formate = DateFormatter()
formate.calendar = Calendar(identifier: .japanese)
formate.dateFormat = "G y"
print(formate.string(from: Date()))

Try above code.

CodePudding user response:

Working backwards I just checked:

let date = Date()
let calendar = Calendar(identifier: .japanese)
let year = calendar.dateComponents([Calendar.Component.year], from: date)
// year == 4

Comparing with https://groups.oist.jp/resource-center/japanese-year-converter that looks correct. I bet that during day to day usage of dates Japanese folks use a Gregorian calendar as well. Seems like maybe this is used for official government documents and such?

EDIT: Yea looks like according to https://simple.wikipedia.org/wiki/Japanese_calendar the Gregorian calendar is used. In general you'd be using Calendar.current for any sort of date display / input from the user, so as long as no assumptions are made about the calendar and storage in the backend / data layer is in a format that accounts for this information you'd be in good shape.

  • Related