Update addWeekDays.md
This commit is contained in:
@ -1,27 +1,27 @@
|
|||||||
---
|
---
|
||||||
title: addWeekDays
|
title: addWeekDays
|
||||||
tags: array,date,intermediate
|
tags: date,array,intermediate
|
||||||
---
|
---
|
||||||
|
|
||||||
Returns a date after adding given number of business days.
|
Returns a date after adding given number of business days.
|
||||||
|
|
||||||
- Use `Array.from()` to construct an array with `length` equal to the `businessDayCount`.
|
- Use `Array.from()` to construct an array with `length` equal to the `count` of business days to be added.
|
||||||
- Use `Array.prototype.reduce()` to iterate over the array, and increment `startDate` using `Date.getDate()` and `Date.setDate()`.
|
- Use `Array.prototype.reduce()` to iterate over the array, starting from `startDate` and incrementing, using `Date.getDate()` and `Date.setDate()`.
|
||||||
- If `startDate` falls on a weekend, update it again by adding either one day or two days to make it a weekday.
|
- If the current `date` is on a weekend, update it again by adding either one day or two days to make it a weekday.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const addWeekDays = (startDate, businessDayCount) =>
|
const addWeekDays = (startDate, count) =>
|
||||||
Array
|
Array
|
||||||
.from({ length: businessDayCount })
|
.from({ length: businessDayCount })
|
||||||
.reduce(date => {
|
.reduce(date => {
|
||||||
date = new Date(date.setDate(date.getDate() + 1));
|
date = new Date(date.setDate(date.getDate() + 1));
|
||||||
if (date.getDay() % 6 === 0)
|
if (date.getDay() % 6 === 0)
|
||||||
date = new Date(date.setDate(date.getDate() + ((date.getDay() / 6) + 1)));
|
date = new Date(date.setDate(date.getDate() + ((date.getDay() / 6) + 1)));
|
||||||
return date;
|
return date;
|
||||||
}, startDate);
|
}, startDate);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
addWeekDays(new Date("Oct 09, 2020"), 5); // 'Oct 16, 2020'
|
addWeekDays(new Date('Oct 09, 2020'), 5); // 'Oct 16, 2020'
|
||||||
addWeekDays(new Date("Oct 12, 2020"), 5); // 'Oct 19, 2020'
|
addWeekDays(new Date('Oct 12, 2020'), 5); // 'Oct 19, 2020'
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user