## How to calculate Number of 'Working days' between two dates in Javascript using moment.js?

how to calculate percentage of a number
how to find the percentage of two numbers
percentage formula
how to calculate percentage of marks
how to calculate percentage increase
how to calculate percentage with calculator
how to calculate 80% of a number
calculation example

How to calculate the number of working days between two dates in JavaScript using moment.js. I have a working formula that calculate these days, but the formula does not satisfies all 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 per 7 days here because saturday and sunday are considered as NON-Working days, but this formula will fail if the count of the days were started from sunday or saturday.

Please any one could help in this regard that what necessary changes must be done in order to avoid this problem.

Just divide it into 3 parts.. first week, last week and the in between, 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 + Math.floor(days) + wlast; // get the total
} //              ^ EDIT: if days count less than 7 so no decimal point
```

The test 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)');
}
}
```

Output of test code:

```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)
```

Percentage Calculator, The percentage difference between two values is calculated by dividing the absolute value of the difference between two numbers by the average of those two  The easiest way to find the number of protons, neutrons, and electrons for an element is to look at the element’s atomic number on the periodic table. That number is equal to the number of protons. The number of protons is equal to the number of electrons, unless there’s an ion superscript listed after the element.

I use a simple function to accomplish that. Maybe is not the most efficient but it works. It doesn't 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;
}
```

To `addDays`, I use the following function:

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

How to Work Out a Percentage Using a Calculator, Instruction and guidance on how to calculate percentage values include it is customizable so you can set the percentage and the numbers to whatever you  Assign each letter a digit from one to nine. Start with the letter A. Write a ‘1’ next to it and then give each following letter the next number as you go in numerical order. For example, B will have a 2, and C is 3. Once you reach ‘I’ which is a ‘9,’ start back at 1 as you continue along the alphabet.

I've made an adaptation to Kokizzu answer, in order to fix a Summer Time problem. In Brazilian Timezone (GMT -3), the difference between 17/10/2017 and 18/10/2017 was -2.71 instead of 2.

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

The end of the week was 22/10/2017 00:00 UTC or 21/10/2017 22:00-02:00 (Summer Time)

Therefore, instead of 7, the difference between "first" and "last" variable in days was 6 (or 8, depending on your Timezone).

The code fixed is 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)
```

Percentage Calculator, Find the percentage change between two values or calculate a percent of a value before and after. In mathematics, a percentage is a number in the fraction of  The calculator determines the underlying or basic meaning. That's its numerology an essence. The practical meaning or interpretation of the meaning comes when the number is related to something, such as a telephone number, birthday, vacation, or when making a decision represented by the number.

I found kokizzu's answer didn't work if days were in the same week (e.g. sun to sat), so settled for this instead :

```    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;
}
```

How to Calculate Percentages, Learning how to calculate the percentage of one number vs. another number is easy. If you want to know what percent A is of B, you simple  Thus, if we want to calculate the probability of drawing an ace from a standard deck of playing cards, we can divide the number of outcomes in the event where an ace is drawn (4) by the total number of possible outcomes where any card is drawn (52).

By using momentjs, it's possible this way:

```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;
}
```

How to calculate percentages, Percentage Calculator is a free online tool to calculate percentages. What is % of ? %. is what percent of ? %. What is the percentage increase/decrease from to To calculate the number of atoms in a sample, divide its weight in grams by the amu atomic mass from the periodic table, then multiply the result by Avogadro's number: 6.02 x 10^23. Set up Equation Express the relationship of the three pieces of information you need to calculate the number of atoms in the sample in the form of an equation.

Percentage calculator, To calculate the percentage of a number Y that X represents, just divide X by Y and multiply the result by 100. You may have to round off to a certain number of  To calculate the percentage of a specific number, you first convert the percentage number to a decimal. This process is the reverse of what you did earlier. You divide your percentage by 100.

Percentage Calculator, One of the quickest ways to calculate a percentage with a calculator is to convert the percentage into a decimal and multiply it by the number you're trying to find  Calculate a negative exponent using inversion. When the exponent is a negative number, you are using the inverse of the number. For example, three to the power of negative four, or 3^-4, will be equal to one over three to the power of positive four (1/3 4) or 1/ 3 x 3 x 3 x 3 or 1/81.

How Do You Find Out a Percent of a Number?, How to find percent? Online calculator: Find Percent. Online Calculators: Calculate Percentage. Find Percent of Two numbers example: You need to find out  Divide the number of blocks being installed by 33.3 to calculate how many bags are needed. Standard mortar requires a 1:3 cement to sand mix, which amounts to 1 yd 3 of sand for every seven bags of cement.

##### Comments
• For a date range of 5 days, it gives 5.7 days.
• In fact, for a date range of less than 7 days, the value is in decimal figures.
• I had the same problem. It was due to Summer Time differences between the beginning and the end of the week. The fix is below.
• Just add `Math.floor(days)` would solve the problem '__') thanks for spotting that out guys
• Doesn't work across different months.
• works fine across multiple months for me... perhaps you can share your code / error ?
• Welcome to SO! Answers are only permitted in English on stackoverflow.com. For more information, read this post
• Doesn't work across months