[PYTHON] Method to convert between Western calendar and Japanese calendar

background

--I just wrote

code

def convert_year_to_wareki(year):
    wareki_start_year = {
        'Meiji': 1868, 'Taisho': 1912, 'Showa': 1926, 'Heisei': 1989, 'Reiwa': 2019
    }
    if year < wareki_start_year['Taisho']:
        return f"(Meiji{year - wareki_start_year['Meiji'] + 1}Year)"
    elif year == wareki_start_year['Taisho']:
        return f"(Meiji{year - wareki_start_year['Meiji'] + 1}Year/Taisho{year - wareki_start_year['Taisho'] + 1}Year)"
    elif year < wareki_start_year['Showa']:
        return f"(Taisho{year - wareki_start_year['Taisho'] + 1}Year)"
    elif year == wareki_start_year['Showa']:
        return f"(Taisho{year - wareki_start_year['Taisho'] + 1}Year/Showa{year - wareki_start_year['Showa'] + 1}Year)"
    elif year < wareki_start_year['Heisei']:
        return f"(Showa{year - wareki_start_year['Showa'] + 1}Year)"
    elif year == wareki_start_year['Heisei']:
        return f"(Showa{year - wareki_start_year['Showa'] + 1}Year/Heisei{year - wareki_start_year['Heisei'] + 1}Year)"
    elif year < wareki_start_year['Reiwa']:
        return f"(Heisei{year - wareki_start_year['Heisei'] + 1}Year)"
    elif year == wareki_start_year['Reiwa']:
        return f"(Heisei{year - wareki_start_year['Heisei'] + 1}Year/Reiwa{year - wareki_start_year['Reiwa'] + 1}Year)"
    else:
        return f"(Reiwa{year - wareki_start_year['Reiwa'] + 1}Year)"

reference

   year2Wareki(year){
      let wYear = ''
      let gen = ''
      if(year > 2018){
        wYear = year-2018
        gen = 'Reiwa'
      }else if(year > 1988){
        wYear= year-1988
        gen = 'Heisei'
      }else if(year > 1925){
        wYear = year-1925
        gen = 'Showa'
      }else if(year > 1911){
        wYear = year-1911
        gen = 'Taisho'
      }else if(year > 1867){
        wYear = year-1867
        gen = 'Meiji'
      }
      if(wYear === 1) wYear = 'Former'
      return gen !== '' ? gen + wYear + 'Year' : false
    },
def convert_to_wareki(d: datetime.date) -> str:
    dt_native = datetime.datetime.combine(d, datetime.time())
    return strjpftime(dt_native, f"{d.year}Year(%O%EYear) {d.month}Month{d.day}Day")
2020(2020)January 1

Recommended Posts

Method to convert between Western calendar and Japanese calendar
Python practice Convert the input year to the Japanese calendar
Script to convert between Xcode language files and tab-delimited text
Program to convert Japanese to station name
[Python] Difference between function and method
[Python] Difference between class method and static method
Difference in writing method to read external source code between Ruby and Python
Convert DICOM to PNG with Ascending and Descending
How to convert SVG to PDF and PNG [Python]
How to switch between Linux and Mac shells
Convert to HSV
Convert timezoned date and time to Unixtime in Python2.7
[Python] Convert decimal numbers to binary numbers, octal numbers, and hexadecimal numbers
[Python] Convert general-purpose container and class to each other
[Python] Created a method to convert radix in 1 second
How to use argparse and the difference between optparse