Adding working days with Javascript

How can I use javascriptto add business days (etc. - Friday), automatically adding days off when necessary?

So, if I added 5 business days to today (Tue. November 22, 2016), I would like to receive Tue. 29th Nov. 2016, not Sun. 27th Nov. 2016.

+6
source share
3 answers

You can use the function Date setDate(in conjunction with getDate) to add days to a date, i.e.

var myDate = new Date(); // Tue 22/11/2016
myDate.setDate(myDate.getDate() + 3); // Fri 25/11/2016

Therefore, by calculating the number of days off in the period of working days, you can add it and the required number of working days to the start date to get the end date.

, , -

function addWorkDays(startDate, days) {
    if(isNaN(days)) {
        console.log("Value provided for \"days\" was not a number");
        return
    }
    if(!(startDate instanceof Date)) {
        console.log("Value provided for \"startDate\" was not a Date object");
        return
    }
    // Get the day of the week as a number (0 = Sunday, 1 = Monday, .... 6 = Saturday)
    var dow = startDate.getDay();
    var daysToAdd = parseInt(days);
    // If the current day is Sunday add one day
    if (dow == 0)
        daysToAdd++;
    // If the start date plus the additional days falls on or after the closest Saturday calculate weekends
    if (dow + daysToAdd >= 6) {
        //Subtract days in current working week from work days
        var remainingWorkDays = daysToAdd - (5 - dow);
        //Add current working week weekend
        daysToAdd += 2;
        if (remainingWorkDays > 5) {
            //Add two days for each working week by calculating how many weeks are included
            daysToAdd += 2 * Math.floor(remainingWorkDays / 5);
            //Exclude final weekend if remainingWorkDays resolves to an exact number of weeks
            if (remainingWorkDays % 5 == 0)
                daysToAdd -= 2;
        }
    }
    startDate.setDate(startDate.getDate() + daysToAdd);
    return startDate;
}

//And use it like so (months are zero based)
var today = new Date(2016, 10, 22);
today = addWorkDays(today, 5); // Tue Nov 29 2016 00:00:00 GMT+0000 (GMT Standard Time)

Date -

Date.prototype.addWorkDays = function (days) {
    if(isNaN(days)) {
        console.log("Value provided for \"days\" was not a number");
        return
    }

    // Get the day of the week as a number (0 = Sunday, 1 = Monday, .... 6 = Saturday)
    var dow = this.getDay();
    var daysToAdd = parseInt(days);
    // If the current day is Sunday add one day
    if (dow == 0) {
        daysToAdd++;
    }
    // If the start date plus the additional days falls on or after the closest Saturday calculate weekends
    if (dow + daysToAdd >= 6) {
        //Subtract days in current working week from work days
        var remainingWorkDays = daysToAdd - (5 - dow);
        //Add current working week weekend
        daysToAdd += 2;
        if (remainingWorkDays > 5) {
            //Add two days for each working week by calculating how many weeks are included
            daysToAdd += 2 * Math.floor(remainingWorkDays / 5);
            //Exclude final weekend if the remainingWorkDays resolves to an exact number of weeks
            if (remainingWorkDays % 5 == 0)
                daysToAdd -= 2;
        }
    }
    this.setDate(this.getDate() + daysToAdd);
};

//And use it like so (months are zero based)
var today = new Date(2016, 10, 22)
today.addWorkDays(5); // Tue Nov 29 2016 00:00:00 GMT+0000 (GMT Standard Time)
+6

, moment-business-days.

:

// 22-11-2016 is Tuesday, DD-MM-YYYY is the format 
moment('22-11-2016', 'DD-MM-YYYY').businessAdd(5)._d // Tue Nov 29 2016 00:00:00 GMT-0600 (CST) 
0

const date = new Date('2000-02-02')
const daysToAdd = mapToWorkdays(date, 37)

date.setUTCDate(date.getUTCDate() + daysToAdd)

console.log( date.toISOString().split('T')[0] )
// prints 2000-03-24

/**
 * @param {Date} date starting date
 * @param {number} add number of workdays to add
 * @return {number} total number of days to add to reach correct date
 */
function mapToWorkdays(date, add) {
  const wd = weekday(date)
  
  let r = Math.trunc(add / 5) * 2
  const rem = add % 5

  if (wd > 4) r += (6-wd)
  else if (wd+rem > 4) r += 2

  return add + r
}

/**
 * @param {Date} date
 * @return {number} day of the week in range of 0..6 (monday..sunday)
 */
function weekday(date) { return (date.getUTCDay()+ 6) % 7 }
Run code
0
source

Source: https://habr.com/ru/post/1661564/


All Articles