回答編集履歴

3

2021/09/27 13:37

投稿

mayu-
mayu-

スコア335

test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  const inc = (weekdays > 0) ? 1: -1;
38
38
 
39
- const org = myholidays.flat().map( d => new Date(d));
39
+ const org = myholidays.flat().filter(f => f).map(d => new Date(d));
40
40
 
41
41
 
42
42
 

2

2021/09/27 13:37

投稿

mayu-
mayu-

スコア335

test CHANGED
@@ -36,7 +36,7 @@
36
36
 
37
37
  const inc = (weekdays > 0) ? 1: -1;
38
38
 
39
- const org = myholidays.map( d => new Date(d));
39
+ const org = myholidays.flat().map( d => new Date(d));
40
40
 
41
41
 
42
42
 

1

会社の独自休日を判別できるようにロジックを変更

2021/09/27 12:53

投稿

mayu-
mayu-

スコア335

test CHANGED
@@ -24,9 +24,11 @@
24
24
 
25
25
  引数2: N日後(前)の数値
26
26
 
27
+ 引数3: 会社独自の休日を格納した配列(省略可)
28
+
27
29
  */
28
30
 
29
- function haruna_weekday(targetdate, weekdays = 0) {
31
+ function haruna_weekday(targetdate, weekdays = 0, myholidays = []) {
30
32
 
31
33
  if (weekdays === 0) return new Date(targetdate);
32
34
 
@@ -34,11 +36,13 @@
34
36
 
35
37
  const inc = (weekdays > 0) ? 1: -1;
36
38
 
39
+ const org = myholidays.map( d => new Date(d));
40
+
41
+
42
+
37
43
  let count = 0;
38
44
 
39
- let date = new Date(targetdate);
45
+ let date = new Date(targetdate);
40
-
41
-
42
46
 
43
47
  while (count !== weekdays) {
44
48
 
@@ -48,11 +52,13 @@
48
52
 
49
53
  if (cal.getEventsForDay(date).length) continue; // 祝日判定
50
54
 
55
+ if (org.some( d => d.getTime() === date.getTime())) continue; // 独自休日判定
56
+
51
57
  count += inc;
52
58
 
53
59
  }
54
60
 
55
- return date;
61
+ return date;
56
62
 
57
63
  }
58
64
 
@@ -86,6 +92,12 @@
86
92
 
87
93
  console.log(haruna_weekday(new Date('2021/10/1'), 10));
88
94
 
95
+
96
+
97
+ // 2021年10月1日(金)の 10営業日後(会社の独自休日2日含む)は 2021年10月19日(火)
98
+
99
+ console.log(haruna_weekday('2021/10/1', 10, ['2021/10/6','2021/10/12']));
100
+
89
101
  ```
90
102
 
91
103