Count weekdays between two dates in JavaScript
Advanced · 4 min read
"Your refund will arrive within 5 working days." "This task has 3 working days left." Payroll, delivery estimates and project plans all count Monday-to-Friday days and skip the weekend. Counting them sounds easy, and the simple version is right on most computers. On some, it quietly loses a day.
In this tutorial you will write workingDaysBetween(from, to). It counts the weekdays from one date to another, including both ends, and returns 0 for a backwards range. You will also see the version almost everyone writes first, and why it is one day short in some time zones. You need to know getDay(), which gives 0 for Sunday and 6 for Saturday, and how to make a date from parts.
Counting both ends
From Monday 12 January to Friday 16 January 2026 there are 5 working days: the 12th, 13th, 14th, 15th and 16th. But the gap between the two dates is only 4 days. When both ends count, the number of days is the gap plus one. Forgetting that + 1 is the classic off-by-one mistake.
Why use UTC here
Every date in JavaScript has two views: your local time, which follows your computer's time zone, and UTC, the world's reference clock. In many places the clocks move by an hour twice a year for daylight saving time. On those days a local day is 23 or 25 hours long.
UTC never does that. Every UTC day is exactly 24 hours, or 86,400,000 milliseconds. So if we step through days in UTC, every step is the same size and nothing can drift.
Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.
46 Working days between two dates
hardCount the Monday-to-Friday days from one date to another, counting both ends. A reversed range counts as none.
Stuck? Three hints, smallest first
- 1. Work out how many days the range covers, then look at the weekday of each one.
- 2. Step through the days in UTC. A local-midnight cursor is not safe: in a zone that changes its clocks at midnight it drifts to 01:00 and the last day gets missed.
- 3.
getUTCDay()on a UTC-midnight instant is the weekday of that calendar date.
Step 1: is this day a weekend?
const MS_PER_DAY = 86400000;
const isWeekendDay = (weekday) => weekday === 0 || weekday === 6;
console.log(isWeekendDay(6)); // true
console.log(isWeekendDay(1)); // false
The function takes a weekday number, not a date. That lets us use it with either getDay() or getUTCDay().
Step 2: turn each date into its UTC calendar day
Date.UTC(year, month, day) gives the timestamp of midnight UTC on that calendar date. Feeding it the local year, month and day keeps the calendar date and throws away the time:
const utcDay = (date) => Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
console.log(utcDay(new Date(2026, 0, 12, 23, 59)) === utcDay(new Date(2026, 0, 12))); // true
1 minute to midnight and midnight itself give the same value. The time of day no longer matters, which is what we want when counting days.
Step 3: how many days does the range cover?
const start = utcDay(new Date(2026, 0, 12)); // Monday 12 January
const end = utcDay(new Date(2026, 0, 16)); // Friday 16 January
console.log((end - start) / MS_PER_DAY + 1); // 5
The gap is 4 days, plus 1 because both ends count. Because both values are UTC midnights, the division always gives a whole number.
Step 4: look at each day
Now walk through the days and count the ones that aren't weekends. new Date(start + i * MS_PER_DAY) is the ith day of the range, and getUTCDay() reads its weekday in UTC:
function workingDaysBetween(from, to) {
const start = utcDay(from);
const end = utcDay(to);
if (end < start) return 0;
const span = (end - start) / MS_PER_DAY + 1;
let count = 0;
for (let i = 0; i < span; i++) {
if (!isWeekendDay(new Date(start + i * MS_PER_DAY).getUTCDay())) count++;
}
return count;
}
const monday = new Date(2026, 0, 12);
const friday = new Date(2026, 0, 16);
console.log(workingDaysBetween(monday, friday)); // 5
console.log(workingDaysBetween(monday, new Date(2026, 0, 18))); // 5
console.log(workingDaysBetween(monday, new Date(2026, 0, 19))); // 6
Adding the weekend of the 17th and 18th adds nothing. The Monday after brings it to six.
The if (end < start) return 0 line handles a backwards range. Without it, span would be negative, the loop would never run, and you would get 0 anyway. Saying it out loud makes the rule clear to the next reader.
Step 5: the edge cases
console.log(workingDaysBetween(friday, friday)); // 1
console.log(workingDaysBetween(new Date(2026, 0, 17), new Date(2026, 0, 17))); // 0
console.log(workingDaysBetween(friday, monday)); // 0
console.log(workingDaysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31))); // 22
console.log(workingDaysBetween(new Date(2026, 0, 12, 23, 59), new Date(2026, 0, 16, 0, 1))); // 5
A single Friday counts as 1, a single Saturday as 0. January 2026 has 22 working days. The last line shows the times of day are ignored.
Step 6: the wrong turn, a local cursor
Here is the version most people write first. It moves a local date forward one day at a time until it passes the end:
function localCursorVersion(from, to) {
const last = new Date(to.getFullYear(), to.getMonth(), to.getDate());
const cursor = new Date(from.getFullYear(), from.getMonth(), from.getDate());
let count = 0;
while (cursor <= last) {
if (!isWeekendDay(cursor.getDay())) count++;
cursor.setDate(cursor.getDate() + 1);
}
return count;
}
console.log(localCursorVersion(new Date(2026, 0, 1), new Date(2026, 11, 31))); // 261 in Lagos, 260 in Santiago
In Lagos it gives 261, the right answer. On a computer set to Santiago, Chile, it gives 260. Chile moves its clocks forward at midnight, so in 2026 there is no midnight on 6 September: the clock jumps straight to 01:00. The cursor lands on 01:00 that day and keeps that hour for the rest of the year. On 31 December, 01:00 is later than last, which is midnight, so the loop stops one day early.
The UTC version can't do this, because every UTC day starts at a real midnight.
The full solution
The panel has workingDaysBetween and, for comparison, localCursorVersion. It prints a working week, January, and the whole of 2026 both ways. The checks cover weekends, single days, backwards ranges, the year's 261 working days and times of day. Press Run to see them.
const MS_PER_DAY = 86400000;
const isWeekendDay = (weekday) => weekday === 0 || weekday === 6;
// Both ends re-expressed as UTC midnight, so every step below is exactly 24 hours.
const utcDay = (date) => Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
function workingDaysBetween(from, to) {
const start = utcDay(from);
const end = utcDay(to);
if (end < start) return 0; // a backwards range spans nothing
const span = (end - start) / MS_PER_DAY + 1; // + 1 because both ends count
let count = 0;
for (let i = 0; i < span; i++) {
if (!isWeekendDay(new Date(start + i * MS_PER_DAY).getUTCDay())) count++;
}
return count;
}
// The version that looks equivalent and is not: a local cursor compared against
// a local end date. Correct in a fixed-offset zone, one day short in Santiago or
// Havana, where the clocks change AT midnight and the cursor drifts to 01:00.
function localCursorVersion(from, to) {
const last = new Date(to.getFullYear(), to.getMonth(), to.getDate());
const cursor = new Date(from.getFullYear(), from.getMonth(), from.getDate());
let count = 0;
while (cursor <= last) {
if (!isWeekendDay(cursor.getDay())) count++;
cursor.setDate(cursor.getDate() + 1);
}
return count;
}
// January 2026: the 12th is a Monday, the 16th a Friday, the 17th a Saturday.
const monday = new Date(2026, 0, 12);
const friday = new Date(2026, 0, 16);
console.log('Mon 12 to Fri 16:', workingDaysBetween(monday, friday));
console.log('Mon 12 to Sun 18:', workingDaysBetween(monday, new Date(2026, 0, 18)));
console.log('all of January :', workingDaysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31)));
console.log('all of 2026, in UTC:', workingDaysBetween(new Date(2026, 0, 1), new Date(2026, 11, 31)));
console.log('all of 2026, local cursor:', localCursorVersion(new Date(2026, 0, 1), new Date(2026, 11, 31)), '<- 260 in America/Santiago');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a full working week', workingDaysBetween(monday, friday), 5);
test('adding the weekend adds nothing', workingDaysBetween(monday, new Date(2026, 0, 18)), 5);
test('the Monday after brings it to six', workingDaysBetween(monday, new Date(2026, 0, 19)), 6);
test('one weekday counts as one, because both ends are included', workingDaysBetween(friday, friday), 1);
test('a single Saturday counts as none', workingDaysBetween(new Date(2026, 0, 17), new Date(2026, 0, 17)), 0);
test('January 2026 has 22 working days', workingDaysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31)), 22);
test('a backwards range is zero, not negative', workingDaysBetween(friday, monday), 0);
test('the whole of 2026 has 261', workingDaysBetween(new Date(2026, 0, 1), new Date(2026, 11, 31)), 261);
test('and the times of day are ignored', workingDaysBetween(new Date(2026, 0, 12, 23, 59), new Date(2026, 0, 16, 0, 1)), 5);
Stepping day by day is O(n) and completely clear, which for a range of days or months is the right trade. There is a closed-form version — whole weeks times five, plus the leftover days that are not weekends — worth writing once, but fiddly enough that most people get it wrong in one direction or the other.
localCursorVersion is in the panel because it is the version almost everyone writes first, and it is wrong. On a machine set to Santiago — or in Node with TZ=America/Santiago — it returns 260 working days for 2026 instead of 261. Chile moves its clocks at midnight, so local midnight on one day of the year does not exist; the cursor is normalised to 01:00, keeps that hour for the rest of the year through setDate, and then fails the cursor <= last test on the final day. One day silently lost, in one time zone, at one time of year — the hardest class of bug to find by testing on your own machine.
Stepping in UTC removes the possibility: every UTC day is 24 hours, and getUTCDay() of a UTC midnight is the weekday of that calendar date. Both ends are reduced to their calendar date first, so 23:59 on the first day and 00:01 on the last still give five days rather than three. Note finally that real working days need a holiday list; this counts weekends only, and a business-day calculation without holidays is wrong in every country that has any.
Common mistakes
- Forgetting the
+ 1. Monday to Friday is a gap of 4 days but 5 working days when both ends count. - Stepping a local date and comparing it with the end. In a zone that changes its clocks at midnight, the cursor drifts by an hour and the last day is missed.
- Calling this "business days" in a real app. It skips weekends only. Real working days also skip public holidays, so you need a holiday list too.
Try it yourself
- Add a
holidaysparameter, an array of dates, and skip those days too. - Count the working days in each month of 2026 and print them in a list.
- Write
weekendDaysBetween(from, to)using the same loop.