DEV Community

wenrei
wenrei

Posted on

ES6: Date - Increment Days

Q: Given that normal working days are Monday to Friday. Implement a function to increment a date by X working days.

A: Date.getDay() returns a Map of <number, Day>. Use Date.getDay() to check the current day and set the new Date with Date.setDate().

0: Sunday
1: Monday
2: Tuesday
...
...
6: Saturday

 function isWeekend(date: Date) : boolean { return date.getDate() % 6 ===0; } export function incrementAndReturnNextBuisnessDay (date: Date, days: number): Date{ date.setDate(date.getDate() +days); while(isWeekend(date)){ date.setDate(date.getDate() + 1); } return date; 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)