LoginSignup
5
2

More than 5 years have passed since last update.

プレミアムフライデーを求める関数(JavaScript)

Last updated at Posted at 2017-03-08

うちの会社では導入されなかったプレミアムフライデーを求める関数を作ってみました。

コード

対象の翌月から1日引いて対象月の最終日を取得。
最終日が何曜日なのかを判定し、金曜日になるように日数を減算して完了
※ 86400000 = 1000*60*60*24

premium.js
function premium(year, month) {
  const d = new Date(new Date(year, month, 1) - 86400000);
  const w = d.getDay();
  const n = 5 <= w ? w-5 : w+2;
  return new Date(d - n * 86400000);
}

最後に

ただライブラリ無しでやってみたかっただけでした。

追記

コメントで頂いたもっとかっこいい書き方

function premium(year, month) {
    const d = new Date(year, month, 0);
    const w = d.getDay();
    return new Date(year, month, -(w+2)%7);
}
5
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2