2016-11-22 2 views
4

Wie kann ich javascript verwenden, um Arbeitstage (d. H. Mo - Friday) hinzuzufügen und automatisch Wochenenden hinzuzufügen, wo nötig?Hinzufügen von Arbeitstagen mit Javascript

Also wenn ich 5 Arbeitstage zu heute hinzufügen würde (Di. 22. Nov. 2016) Ich möchte Tue. 29th Nov. 2016 und nicht Sun. 27th Nov. 2016 bekommen.

Antwort

6

Es ist möglich, Date ‚s setDate Funktion (in Verbindung mit getDate) zu verwenden Tagen auf ein Datum, dh zu addieren -

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

Also, wenn Sie die Anzahl der Wochentage innerhalb der Werktage Zeit berechnet haben Sie können diese und die erforderliche Anzahl von Arbeitstagen zum Startdatum hinzufügen, um das endgültige Datum zu erhalten.

Diese Funktion sollte jedoch funktionieren offensichtlich nicht wegen nationaler Feiertage nehmen -

function addWorkDays(startDate, days) { 
    // 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's 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) 

Es ist auch an den Date Prototyp hinzugefügt werden könnten -

Date.prototype.addWorkDays = function (days) { 
    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's 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) 
0

Ich glaube, Sie moment-business-days verwenden können.

Beispiel:

// 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) 
Verwandte Themen