How to calculate the number of "working days" between two dates in Javascript using moment.js?

How to calculate the number of working days between two dates in JavaScript using moment.js. I have a working formula that calculates these days, but the formula does not satisfy all the conditions:

here is my code:

var start= moment(data[x].start_date); var end= moment(data[x].est_end_date); var difference= end.diff(start, 'days'); var workingDays= Math.round((difference/7)*5); //data[x] is for iterating over a loop 

I get five days for 7 days here because Saturday and Sunday are treated as non-working days, but this formula fails if the counting of days starts on Sunday or Saturday.

Please anyone can help in this regard that necessary changes must be made to avoid this problem.

+13
javascript date momentjs formula
source share
6 answers

Just divide it into 3 parts. First week, last week and interim, something like this:

 function workday_count(start,end) { var first = start.clone().endOf('week'); // end of first week var last = end.clone().startOf('week'); // start of last week var days = last.diff(first,'days') * 5 / 7; // this will always multiply of 7 var wfirst = first.day() - start.day(); // check first week if(start.day() == 0) --wfirst; // -1 if start with sunday var wlast = end.day() - last.day(); // check last week if(end.day() == 6) --wlast; // -1 if end with saturday return wfirst + days + wlast; // get the total } 

Security Code

 var ftest = {date:'2015-02-0',start:1,end:7}; var ltest = {date:'2015-02-2',start:2,end:8}; var f = 'YYYY-MM-DD'; for(var z=ftest.start; z<=ftest.end; ++z) { var start = moment(ftest.date + z); for(var y=ltest.start; y<=ltest.end; ++y) { var end = moment(ltest.date + y); var wd = workday_count(start,end); console.log('from: '+start.format(f),'to: '+end.format(f),'is '+wd+' workday(s)'); } } 

Test code output:

 from: 2015-02-01 to: 2015-02-22 is 15 workday(s) from: 2015-02-01 to: 2015-02-23 is 16 workday(s) from: 2015-02-01 to: 2015-02-24 is 17 workday(s) from: 2015-02-01 to: 2015-02-25 is 18 workday(s) from: 2015-02-01 to: 2015-02-26 is 19 workday(s) from: 2015-02-01 to: 2015-02-27 is 20 workday(s) from: 2015-02-01 to: 2015-02-28 is 20 workday(s) from: 2015-02-02 to: 2015-02-22 is 15 workday(s) from: 2015-02-02 to: 2015-02-23 is 16 workday(s) from: 2015-02-02 to: 2015-02-24 is 17 workday(s) from: 2015-02-02 to: 2015-02-25 is 18 workday(s) from: 2015-02-02 to: 2015-02-26 is 19 workday(s) from: 2015-02-02 to: 2015-02-27 is 20 workday(s) from: 2015-02-02 to: 2015-02-28 is 20 workday(s) from: 2015-02-03 to: 2015-02-22 is 14 workday(s) from: 2015-02-03 to: 2015-02-23 is 15 workday(s) from: 2015-02-03 to: 2015-02-24 is 16 workday(s) from: 2015-02-03 to: 2015-02-25 is 17 workday(s) from: 2015-02-03 to: 2015-02-26 is 18 workday(s) from: 2015-02-03 to: 2015-02-27 is 19 workday(s) from: 2015-02-03 to: 2015-02-28 is 19 workday(s) from: 2015-02-04 to: 2015-02-22 is 13 workday(s) from: 2015-02-04 to: 2015-02-23 is 14 workday(s) from: 2015-02-04 to: 2015-02-24 is 15 workday(s) from: 2015-02-04 to: 2015-02-25 is 16 workday(s) from: 2015-02-04 to: 2015-02-26 is 17 workday(s) from: 2015-02-04 to: 2015-02-27 is 18 workday(s) from: 2015-02-04 to: 2015-02-28 is 18 workday(s) from: 2015-02-05 to: 2015-02-22 is 12 workday(s) from: 2015-02-05 to: 2015-02-23 is 13 workday(s) from: 2015-02-05 to: 2015-02-24 is 14 workday(s) from: 2015-02-05 to: 2015-02-25 is 15 workday(s) from: 2015-02-05 to: 2015-02-26 is 16 workday(s) from: 2015-02-05 to: 2015-02-27 is 17 workday(s) from: 2015-02-05 to: 2015-02-28 is 17 workday(s) from: 2015-02-06 to: 2015-02-22 is 11 workday(s) from: 2015-02-06 to: 2015-02-23 is 12 workday(s) from: 2015-02-06 to: 2015-02-24 is 13 workday(s) from: 2015-02-06 to: 2015-02-25 is 14 workday(s) from: 2015-02-06 to: 2015-02-26 is 15 workday(s) from: 2015-02-06 to: 2015-02-27 is 16 workday(s) from: 2015-02-06 to: 2015-02-28 is 16 workday(s) from: 2015-02-07 to: 2015-02-22 is 10 workday(s) from: 2015-02-07 to: 2015-02-23 is 11 workday(s) from: 2015-02-07 to: 2015-02-24 is 12 workday(s) from: 2015-02-07 to: 2015-02-25 is 13 workday(s) from: 2015-02-07 to: 2015-02-26 is 14 workday(s) from: 2015-02-07 to: 2015-02-27 is 15 workday(s) from: 2015-02-07 to: 2015-02-28 is 15 workday(s) 
+19
source share

I use a simple function to achieve this. Maybe not the most effective, but it works. This does not require Moment.js. it's just javascript.

 function getNumWorkDays(startDate, endDate) { var numWorkDays = 0; var currentDate = new Date(startDate); while (currentDate <= endDate) { // Skips Sunday and Saturday if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) { numWorkDays++; } currentDate = currentDate.addDays(1); } return numWorkDays; } 

For addDays I use the following function:

 Date.prototype.addDays = function (days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }; 
+6
source share

I found that the kokizzu answer did not work if the days were in the same week (for example, the sun to sit), so I decided for this:

  function calcBusinessDays(startDate, endDate) { var day = moment(startDate); var businessDays = 0; while (day.isSameOrBefore(endDate,'day')) { if (day.day()!=0 && day.day()!=6) businessDays++; day.add(1,'d'); } return businessDays; } 
+3
source share

I made an adaptation to Kokitstsa's answer to solve the problem of daylight saving time. In the Brazilian time zone (GMT -3), the difference between 10/17/2017 and 10/18/2017 was -2.71 instead of 2.

The beginning of the week was 10/15/2017 00:00 UTC or 10/14/2017 21: 00-03: 00

The end of the week was 10/22/2017 00:00 UTC or 10/21/2017 10:00 p.m. - 2:00 a.m. (summer time)

Therefore, instead of 7, the difference between the “first” and “last” variables in days was 6 (or 8, depending on your time zone).

The code is fixed below:

 start = moment(start).utc().add(start.utcOffset(), 'm'); // Ignore timezones end = moment(end).utc().add(end.utcOffset(), 'm'); // Ignore timezones var first = start.clone().endOf('week'); // end of first week var last = end.clone().startOf('week'); // start of last week // Fixing Summer Time problems firstCorrection = moment(first).utc().add(60, 'm').toDate(); // var days = last.diff(firstCorrection,'days') * 5 / 7; // this will always multiply of 7 var wfirst = first.day() - start.day(); // check first week if(start.day() == 0) --wfirst; // -1 if start with sunday var wlast = end.day() - last.day(); // check last week if(end.day() == 6) --wlast; // -1 if end with saturday return wfirst + days + wlast; // get the total (subtract holidays if needed) 
+3
source share

Using momentjs , this is possible as follows:

 private calculateWorkdays(startDate: Date, endDate: Date): number { // + 1 cause diff returns the difference between two moments, in this case the day itself should be included. const totalDays: number = moment(endDate).diff(moment(startDate), 'days') + 1; const dayOfWeek = moment(startDate).isoWeekday(); let totalWorkdays = 0; for (let i = dayOfWeek; i < totalDays + dayOfWeek; i++) { if (i % 7 !== 6 && i % 7 !== 0) { totalWorkdays++; } } return totalWorkdays; } 
+1
source share

You can try this (without a loop):

  function getBusinessDays(endDate, startDate) { var lastDay = moment(endDate); var firstDay = moment(startDate); let calcBusinessDays = 1 + (lastDay.diff(firstDay, 'days') * 5 - (firstDay.day() - lastDay.day()) * 2) / 7; if (lastDay.day() == 6) calcBusinessDays--;//SAT if (firstDay.day() == 0) calcBusinessDays--;//SUN return calcBusinessDays; } 

Source

0
source share

All Articles