teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

調整

2021/12/06 08:30

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -4,8 +4,15 @@
4
4
  # sample
5
5
  ```javascript
6
6
  const today="2021-12-06";
7
+ // (1)今日の日付
7
8
  const interval=50;
9
+ // (2)求めたいn日後
8
10
  const holiday=["2022-01-01","2022-01-10","2022-02-11","2022-02-23"].map(x=>new Date(x).getTime());
11
+ // (3)祝日のリスト作成
12
+ const d1=new Array(interval*2).fill(null).map((_,x)=>new Date(today).getTime()+60*60*24*1000*(x+1));
13
+ // (4)余裕をもって求めたいn日後の2倍の日付を配列に保持
9
- const d=new Array(interval*2).fill(null).map((_,x)=>new Date(today).getTime()+60*60*24*1000*(x+1)).filter(x=>!holiday.includes(x)&&new Date(x).getDay()>0).map(x=>new Date(x));
14
+ const d2=d1.filter(x=>!holiday.includes(x)&&new Date(x).getDay()>0).map(x=>new Date(x));
15
+ // (5)祝日に含まれるか、日曜日を除く
10
- console.log(d[interval-1]);
16
+ console.log(d2[interval-1]);
17
+ // (6)n日後を取り出すため1引いて参照
11
18
  ```

1

chousei

2021/12/06 08:29

投稿

yambejp
yambejp

スコア117944

answer CHANGED
@@ -1,2 +1,11 @@
1
1
  営業日カレンダーを作っておくしかありません
2
- 祝日はコロコロ変わるものなので計算で導くことは混乱の元にしかなりません
2
+ 祝日はコロコロ変わるものなので計算で導くことは混乱の元にしかなりません
3
+
4
+ # sample
5
+ ```javascript
6
+ const today="2021-12-06";
7
+ const interval=50;
8
+ const holiday=["2022-01-01","2022-01-10","2022-02-11","2022-02-23"].map(x=>new Date(x).getTime());
9
+ const d=new Array(interval*2).fill(null).map((_,x)=>new Date(today).getTime()+60*60*24*1000*(x+1)).filter(x=>!holiday.includes(x)&&new Date(x).getDay()>0).map(x=>new Date(x));
10
+ console.log(d[interval-1]);
11
+ ```