In my application i have two date picker as start date and end date. when user choose start and end date the system will show the days between two dates but excluding the saturday and sunday. How to calculate it by using angularjs?
Asked
Active
Viewed 2,613 times
0
-
I use moment.js https://momentjs.com/ you can add it to your package.json and perhaps use it within your application – Adriano Jan 23 '20 at 03:21
-
Not all places or cultures consider Saturday and Sunday to be non–working or weekend days. – RobG Jan 23 '20 at 23:11
-
The required result depends on whether the dates should be inclusive or not. Is 1 Jan to 1 Jan one business day or none? Is 1 Jan to 2 Jan one day or two? Rules will vary based on business requirements. – RobG Jan 24 '20 at 00:10
-
I have used Angular framework and Moment.js library to implement the solution. It covers all the cases. Live Demo: https://stackblitz.com/edit/weekdays?embed=1&file=src/app/hello.component.ts – Prakash M. Jan 24 '20 at 12:59
2 Answers
0
Does something like this work:
var startDate = new Date("01-10-2020");
var endDate = new Date("01-20-2020");
var nextDay = new Date(startDate);
var cnt = 0;
do {
/*if (nextDay.getDay() >= 1 && nextDay.getDay() <= 5) {
cnt = cnt + 1;
}*/
cnt += (nextDay.getDay() >= 1 && nextDay.getDay() <= 5) ? 1 : 0;
nextDay.setDate(nextDay.getDate() + 1);
} while (nextDay <= endDate);
console.log("Number of week days between " + startDate + " and " + endDate + " = " + cnt);
Here is the fiddler.

sam
- 1,937
- 1
- 8
- 14
-
`new Date("01-10-2020")` returns an invalid Date in Firefox and Safari. Far better to use `new Date(2020, 9, 1)`. – RobG Jan 23 '20 at 23:10
0
You don't want to do an expensive loop over every day to see whether it is Saturday or Sunday. The logic should be as follows:
- Work in UTC so we don't need to worry about time zone
- Calculate total number of calendar weeks. In a single calendar week, there are guaranteed to be 5 weekdays (not weekends == SAT || SUN)
- Calculate the remainder of days. This will be added to the calculation later.
- Determine the "finalAdjustment" by seeing if the remainder falls on weekend days.
- The number of weekdays is (5 * numWeeks) + remainderDays + finalAdjust
(function() {
"use strict";
var SUN = 0;
var MON = 1;
var TUE = 2;
var WED = 3;
var THU = 4;
var FRI = 5;
var SAT = 6;
function isWeekendDay(day) {
return day === SAT || day === SUN;
}
function numberWeekDays(start, end) {
var numCalendarDays = (end - start) / 1000 / 60 / 60 / 24;
var numWeeks = Math.floor(numCalendarDays / 7);
// Potential days to add on to the number of full calendar
// weeks. This will be adjusted by "finalAdjust"
var remainderDays = numCalendarDays % 7;
// Adjustments for start and end dates being on a weekend
// ----------------------------
// Start at one because the same day should count as 1
// but number of days between same day is 0 based on
// arithmetic above.
// Change this to 0 if you don't want end date inclusive...
var finalAdjust = 1;
var startDay = start.getUTCDay();
var endDay = end.getUTCDay();
// On a weekend, so adjust by subtracting 1
if (isWeekendDay(startDay)) {
finalAdjust--;
}
// On a weekend, so adjust by subtracting 1
if (isWeekendDay(endDay)) {
finalAdjust--;
}
// This accounts for subtracting an extra weekend when starting
// at the beginning of a weekend (e.g. Saturday into Monday)
// The end day cannot also be on a weekend based on week modular division (mod 7)
if (startDay === SAT && remainderDays > 2) {
finalAdjust--;
}
// ---------------------------
// For every full calendar week there are 5 week days
// Use that number with the remainderDays and finalAdjust above
// to arrive at the answer.
var numWeekDays = (5 * numWeeks) + remainderDays + finalAdjust;
return numWeekDays;
}
// Test cases
// Assume that the start and end dates are inclusive
// 2020-01-01 to 2020-01-01 is one day
// 2020-01-01 to 2020-01-02 is two days
// ----------------------
// A Wednesdday
var start = new Date("2020-01-08");
// A Saturday
var end = new Date("2020-02-01");
// Expected answer: 18
console.log(numberWeekDays(start, end));
// A Saturday
start = new Date("2020-01-05");
// A Monday
end = new Date("2020-01-31");
// Expected answer: 20
console.log(numberWeekDays(start, end));
// Weekday to weekday Tuesday to
start = new Date("2020-01-07");
end = new Date("2020-01-16");
// Expected: 8
console.log(numberWeekDays(start, end));
// Same week: Mon-Wed
start = new Date("2020-01-06");
end = new Date("2020-01-08");
// Expected answer: 3
console.log(numberWeekDays(start, end));
// Same day
start = new Date("2020-01-08");
end = new Date("2020-01-08");
// Expect: 1
console.log(numberWeekDays(start, end));
// Weekend only
start = new Date("2020-01-04");
end = new Date("2020-01-05");
// Expect: 0;
console.log(numberWeekDays(start, end));
// ------------------
}());
As others have stated, a date library like moment is useful here because it gives you a lot of utility functions for working with dates and durations.

Patrick
- 6,828
- 3
- 23
- 31
-
This answer assumes that the days are inclusive of the end dates, so 1Jan 2020 to 2 Jan 2020 is 2 business days. – RobG Jan 24 '20 at 00:11
-
I state that clearly in the comments. I'm modifying the algorithm anyway to be more efficient. – Patrick Jan 24 '20 at 00:22
-
One option is to add an optional "inclusive" flag that defaults to true. It's always a fraught question as I think most consider Monday to Friday to be 5 days, but not Monday to Monday to be 1 day. :-) – RobG Jan 24 '20 at 00:44
-
How many days is Monday? Sure I could have an "include" flag, but this should have been enough for somebody to figure out. – Patrick Jan 25 '20 at 15:39