Skip to content

Instantly share code, notes, and snippets.

@sironekotoro
Created August 5, 2020 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sironekotoro/dd6c10330ba6d0edf7f7fc698f2a4ffb to your computer and use it in GitHub Desktop.
Save sironekotoro/dd6c10330ba6d0edf7f7fc698f2a4ffb to your computer and use it in GitHub Desktop.
// https://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html
const holidays = [
"2020/8/10",
"2020/9/21",
"2020/9/22",
"2020/11/3",
"2020/11/23",
"2020/12/29", // 従業員就業規則 第二十条 (4) 年末年始(12月29日〜1月3日)
"2020/12/30", // 同上
"2020/12/31", // 同上
"2021/1/1", // 同上
"2021/1/2", // 同上
"2021/1/3", // 同上
"2021/1/11",
"2021/2/11",
"2021/2/23",
"2021/3/20",
"2021/4/29",
"2021/5/3",
"2021/5/4",
"2021/5/5",
"2021/7/19",
"2021/8/11",
"2021/9/20",
"2021/9/23",
"2021/10/11",
"2021/11/3",
"2021/11/23",
// "2020/10/30", // テスト用
];
/**
* 指定した月の最後の平日を返すカスタム関数
*
* @param {Date}
*
*/
function LAST_WEEKDAY(date) {
const dt = new Date(date);
// 月末を求める
let lastDay = new Date(dt.getFullYear(), dt.getMonth() + 1, 0);
// 平日じゃなかったら平日になるまでループ(0:日曜, 6:土曜)
// 祝日に当たったら、平日になるまでループ
while (
lastDay.getDay() === 0 ||
lastDay.getDay() === 6
) {
lastDay.setDate(lastDay.getDate() - 1);
}
// console.log(lastDay);
return lastDay;
}
/**
* 指定した月の最後の平日(日本の祝日&年末年始休暇考慮)を返すカスタム関数
*
* @param {Date} 日付
*
*/
function LAST_BUSINESSDAY(date) {
const dt = new Date(date);
// 月末を求める
let lastDay = new Date(dt.getFullYear(), dt.getMonth() + 1, 0);
// 平日じゃなかったら平日になるまでループ(0:日曜, 6:土曜)
// 祝日に当たったら、平日になるまでループ
while (
lastDay.getDay() === 0 ||
lastDay.getDay() === 6 ||
isHoliday(lastDay)
) {
lastDay.setDate(lastDay.getDate() - 1);
}
// console.log(lastDay);
return lastDay;
}
/**
* 祝日判定(祝日リストはスクリプトに埋め込み)
*/
function isHoliday(date) {
const stdDate = dateToStr(date);
const bool = holidays.includes(stdDate);
return bool;
}
/**
* Date型を YYYY/MM/DD の文字列に変換する
*/
function dateToStr(date) {
const str = [date.getFullYear(), date.getMonth() + 1, date.getDate()].join(
"/"
);
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment