Skip to content

Instantly share code, notes, and snippets.

@Alex4386
Last active November 16, 2023 05:13
Show Gist options
  • Save Alex4386/fa0ea35dd8027fc416a0b49e0522c792 to your computer and use it in GitHub Desktop.
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!!!)
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;
}
@Alex4386
Copy link
Author

If you are using C#, Just use System.Globalization.JapaneseCalendar. Not this non-sense 🤦

@Alex4386
Copy link
Author

Oh, if you have browser or Javascript runtime modern enough, Don't use this. Try Intl.Locale.getCalendars().

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/getCalendars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment