Last active
November 16, 2023 05:13
-
-
Save Alex4386/fa0ea35dd8027fc416a0b49e0522c792 to your computer and use it in GitHub Desktop.
a javascript getter of Japanese nengo system (which i kinda hate, PLEASE USE ISO 8601!!!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nengos = [{ "name":"reiwa", "baseYear":2019 }, {"name":"heisei", "baseYear":1989}]; | |
// Could you guys just use ISO-8601? PLEASE? | |
function getNengo(date) { | |
// First, Convert to JST | |
const jpDate = new Date(date.setMinutes(date.getMinutes() + (date.getTimezoneOffset() - -540))); | |
// get Year and Months | |
let year = jpDate.getFullYear(); | |
const month = jpDate.getMonth()+1; // counting from 0 | |
if (month > 4) { | |
// new Nengo System is used. Dafaq | |
} else { | |
// prev Nengo System is used. | |
year--; | |
} | |
function inNengoRange(baseYear, year, years) { | |
const workYear = (typeof years === 'undefined') ? 30 : years; | |
return (baseYear <= year && year < baseYear+workYear) | |
} | |
for (const nengo of nengos) { | |
if (inNengoRange(nengo.baseYear, year, nengo.years)) { | |
return nengo.name; | |
} | |
} | |
// Why am i doing this | |
return undefined; | |
} | |
// Due to their ignorant against ISO 8601, It makes me mad that they wrote every single year system as NENGO system. | |
// Which Literally gives me a FUCK | |
function nengo2ISOCompliant(fuck) { | |
const theNengo = fuck.nengo; | |
const theYear = fuck.year; | |
let theNengoBaseYear = 0; | |
for (const nengo of nengos) { | |
if (nengo.name.toLowerCase() === theNengo.toLowerCase()) { | |
theNengoBaseYear = nengo.baseYear; | |
} | |
} | |
return (theYear - 1) + theNengoBaseYear; | |
} |
Oh, if you have browser or Javascript runtime modern enough, Don't use this. Try Intl.Locale.getCalendars()
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you are using C#, Just use
System.Globalization.JapaneseCalendar
. Not this non-sense 🤦