Japanese calendar converter

The Japanese calendar, also known as the Imperial calendar, is based on the reigns of the emperors of Japan. Each era begins with the ascension of a new emperor and ends with the emperor's death or abdication. The current era, Reiwa, began on May 1, 2019, with the ascension of Emperor Naruhito.

Each era is named after a phrase from classical Chinese literature and is numbered sequentially within the emperor's reign. For example, the current era is Reiwa 1 (令和元年, "Reiwa gannen" in Japanese).

The Japanese calendar uses the same months and days as the Gregorian calendar, but the years are numbered differently. In the Japanese calendar, the year is represented by the number of years since the start of the current era. For example, the year 2023 in the Gregorian calendar is Reiwa 5 in the Japanese calendar.

The era from a table that maps era names to their start dates. The start date is given as a year, month, and day in the Gregorian calendar.

Using the start date of the era and the year of the Japanese date, the code calculates the corresponding Gregorian year. For example, if the entered date is in Heisei 10, which started in 1998, and the year is 25, then the Gregorian year is 1998 + 25 - 1 = 2022.

The code then converts the month and day of the Japanese date to integers and creates a new Date object using the calculated Gregorian year, month, and day. If the resulting date is not valid (for example, if the day is greater than the number of days in the month), the code returns an error message. Otherwise, it displays the Gregorian date in the "gregorianDate" paragraph using the toDateString() method of the Date object.

Note that this is a simple implementation that assumes the Japanese date is in the format "era year month day" with full-width characters. It does not handle alternative formats, incomplete dates, or leap years. You can modify the code as needed to handle these cases.

The Japanese calendar uses a system of eras and years that starts with the accession of a new emperor. Each era has a name, such as Meiji, Taisho, Showa, Heisei, or Reiwa, and is numbered sequentially within the emperor's reign. For example, the year 2023 in the Gregorian calendar corresponds to Reiwa 5 in the Japanese calendar.