DEV Community

wenrei
wenrei

Posted on

2 1

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)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more