Skip to content

Instantly share code, notes, and snippets.

@mitsu-ksgr
Created September 16, 2018 18:05
Show Gist options
  • Save mitsu-ksgr/fe0920464f8d4d82406ffcc6b00be03f to your computer and use it in GitHub Desktop.
Save mitsu-ksgr/fe0920464f8d4d82406ffcc6b00be03f to your computer and use it in GitHub Desktop.
干支を返す
/**
* 指定した西暦の干支を返す.
*
* @example
* console.log(`2018: ${getEto(2018)}`); //> '2018: 戊,戌'
*
* @param {number} year - 西暦
* @return {Array<string>} [0] 十干, [1] 十二支
*/
const getEto = (year) => {
if (!Number.isInteger(year)) throw new Error(`getEto Error: a year must be Number. year = ${year}`);
if (year < 0) throw new Error(`getEto Error: a year must be greater than 0. year = ${year}`);
const i10 = ["庚", "辛", "壬", "癸", "甲", "乙", "丙", "丁", "戊", "己"];
const i12 = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"];
// 十二支: 604年から開始らしいので、offsetを考慮する。 offset = -604 % 12 = +8
return [i10[year % 10], i12[(year + 8) % 12]];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment