Search code examples
javascriptdatedatetimeweekday

Get next day, skip weekends


I want to generate next working day using JavaScript.

This is my code as of now

var today = new Date();
    today.setDate(today.getDate());
    var tdd = today.getDate();
    var tmm = today.getMonth()+1;
    var tyyyy = today.getYear();

    var date = new Date();

    date.setDate(date.getDate()+3);

Problem is, on Fridays it returns Saturday's date whereas I want it to be Monday


Solution

  • This will choose the next working day when a date is passed to it.

    I suggest you normalise the date you pass, so you will not be surprised around summertime/wintertime change

    Updated in 2023

    // update and return passed date object to next working day
    const getNextWork = date => {
      let day = date.getDay(), add = 1;
      if (day === 6)           add = 2; else 
      if (day === 5)           add = 3;
      date.setDate(date.getDate() + add); // will correctly handle 31+1 > 32 > 1st next month
      return date;
    };
    
    
    // tests:
    const dt = new Intl.DateTimeFormat("en-US", {
      weekday: "short",
      year: "numeric",
      month: "long",
      day: "numeric",
      timeZone: "UTC",
      timeZoneName: "short",
      hour: "numeric",
      minute: "numeric",
    });
    const aDay = 24 * 60 * 60 * 1000;
    // 26th of March 2023 is daylight savings date in my country
    let date = new Date(2023, 2, 24, 15, 0, 0, 0).getTime();
    
    for (let i = 0; i < 7; i++) {
      const d = new Date(date + i * aDay);
      console.log(dt.format(d), "-->", dt.format(getNextWork(d)));
    }

    If you do not want to update the passed date object have this instead

    // find next working day and return a new date for that date
    const getNewNextWork = date => {
      const dateCopy = new Date(date);
      let day = dateCopy.getDay(), add = 1;
      if (day === 6)           add = 2; else 
      if (day === 5)           add = 3;
      dateCopy.setDate(dateCopy.getDate() + add); // Modifying the copy, not the original date
      return dateCopy;
    };
    

    Older code:

    var today = new Date(2016, 7, 26,12,0,0,0,0); // Friday at noon
    console.log("today, Monday",today,"day #"+today.getDay());
    var next = new Date(today.getTime());
    next.setDate(next.getDate()+1); // tomorrow
    while (next.getDay() == 6 || next.getDay() == 0) next.setDate(next.getDate() + 1);
    console.log("no change    ",next,"day #"+next.getDay());
    console.log("-------");
    // or without a loop:
    
    function getNextWork(d) {
      d.setDate(d.getDate()+1); // tomorrow
      if (d.getDay()==0) d.setDate(d.getDate()+1);
      else if (d.getDay()==6) d.setDate(d.getDate()+2);
      return d;
    }
    next = getNextWork(today); // Friday
    console.log("today, Friday",today);
    console.log("next, Monday ",next);
    console.log("-------");
    today = new Date(2016, 7, 29,12,0,0,0); // Monday at noon
    next = getNextWork(today);              // Still Monday at noon
    console.log("today, Monday",today);
    console.log("no change    ",next);
    console.log("-------");
    
    // Implementing Rob's comment
    
    function getNextWork1(d) {
      var day = d.getDay(),add=1;
      if (day===5) add=3;
      else if (day===6) add=2;
      d.setDate(d.getDate()+add);  
      return d;
    }
    today = new Date(2016, 7, 26,12,0,0,0,0); // Friday at noon
    next = getNextWork1(today);               // Friday
    console.log("today, Friday",today);
    console.log("next, Monday ",next);
    console.log("-------");
    today = new Date(2016, 7, 26,12,0,0,0,0); // Monday at noon
    next = getNextWork1(today); // Monday
    console.log("today, Monday",today);
    console.log("no change    ",next);