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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay