Front-End & Daily

How to count days excluding weekends/holidays or only weekends/holidays in JS

How to count days excluding weekends/holidays or only weekends/holidays in JS

Here's how to count the number of days excluding weekends/holidays or only weekends/holidays using JavaScript or jQuery.

Sample

Please enter a date

is from today

days later, and

there are weekdays,

and weekends/holidays.

You can create a program like this.

Table of Contents

Date Calculation and Holiday Library

First, as preliminary knowledge, let's look at how to calculate dates and how to determine weekends/holidays.

If you simply want to find the difference between two dates,

you can count them this way,

let date1 = new Date('2023-10-01');
let date2 = new Date('2023-10-11');
return Math.ceil((date2 - date1) / 86400000); // 10

However, this time, to determine if it's a weekend or holiday, we need to examine each date individually.

So, we count them roughly this way.

let date1 = new Date('2023-10-01');
let date2 = new Date('2023-10-11');
let count = 0;
//date1 is incremented by 1 as long as date2 is greater
while(date2 >= date1){
	//add 1 day to date1
	date1.setDate(date1.getDate() + 1);
	//get day of the week 0=Sunday 6=Saturday
	let week = date1.getDay();
	//check if it's a holiday (holiday name if it is, undefined if not)
	let is_holiday = JapaneseHolidays.isHoliday(date1);
	//if it's neither a weekend nor a holiday
	if(week != 0 && week != 6 && !is_holiday){
		++count;
	}
}
return count; // 5

Here, we are using this library to determine holidays.

japanese-holidays-js

This way, if it's a holiday, the holiday name will be stored in the variable 'is_holiday'.
If it's not a holiday, 'undefined' will be stored.

<script src="https://cdn.rawgit.com/osamutake/japanese-holidays-js/v1.0.10/lib/japanese-holidays.min.js"></script>
<script>
let date = new Date();
let is_holiday = JapaneseHolidays.isHoliday(date);
</script>

Count only weekdays

You can count only weekdays with code like the following.

//Count weekdays
function weekdays(input){
	//Get today
	let today = new Date();
	//Count
	let count = 0;
	//today is incremented by 1 as long as input is greater than today
	while(input >= today){
		//add 1 day to today
		today.setDate(today.getDate() + 1);
		//get day of the week 0=Sunday 6=Saturday
		let week = today.getDay();
		//check if it's a holiday (holiday name if it is, undefined if not)
		let is_holiday = JapaneseHolidays.isHoliday(today);
		//if it's neither a weekend nor a holiday
		if(week != 0 && week != 6 && !is_holiday){
			++count;
		}
	}
	return count;
}

weekdays(new Date('2023-10-11')); // 8 (if today is 2023-10-01)

The `while` loop iterates through each date, and `count` is incremented only for weekdays.

However, if 'input' is smaller than 'today', meaning it's a past date,
it won't enter the `while` loop and cannot be counted. Please refer to the section How to handle past dates.

Also, as mentioned earlier, don't forget to load 'japanese-holidays-js'.

Count only weekends/holidays

Once you get this far, it's quite simple.

To count only weekends/holidays, change the `if` statement on line 16 of the previous code

to this.

//if it's a weekend or a holiday
if(week == 0 || week == 6 || is_holiday){

Handle both weekdays and weekends/holidays

When you want to count only weekdays and when you want to count only weekends/holidays, you can combine them

into a function like this to handle both.

//Count weekdays or weekends/holidays
function days(input, weekday){
	//Get today
	let today = new Date();
	//Count
	let count = 0;
	//today is incremented by 1 as long as input is greater than today
	while(input >= today){
		//add 1 day to today
		today.setDate(today.getDate() + 1);
		//get day of the week 0=Sunday 6=Saturday
		let week = today.getDay();
		//check if it's a holiday (holiday name if it is, undefined if not)
		let is_holiday = JapaneseHolidays.isHoliday(today);
		//if weekday is true, count weekdays
		if(weekday){
			//if it's neither a weekend nor a holiday
			if(week != 0 && week != 6 && !is_holiday){
				++count;
			}
		}else{
			//if it's a weekend or a holiday
			if(week == 0 || week == 6 || is_holiday){
				++count;
			}
		}
	}
	return count;
}

days(new Date('2023-10-11'), true); // To count only weekdays
days(new Date('2023-10-11'), false); // To count only weekends/holidays

How to handle past dates

Above, we counted 'How many weekdays are there until (a future date)?' To handle past dates as well, you'll likely need to add a conditional branch and another `while` loop.

So, here's the code that also handles past dates.

//Count weekdays
function weekdays(input){
	let today = new Date();
	let count = 0;
	//if the input date is in the future
	if(input - today > 0){
		while(input >= today){
			//add 1 to today
			today.setDate(today.getDate() + 1);
			//get day of the week 0=Sunday 6=Saturday
			let week = today.getDay();
			//check if it's a holiday (holiday name if it is, undefined if not)
			let is_holiday = JapaneseHolidays.isHoliday(today);
			//if it's neither a weekend nor a holiday
			if(week != 0 && week != 6 && !is_holiday){
				++count;
			}
		}
	}else{
		while(input <= today){
			//subtract 1 from today
			today.setDate(today.getDate() - 1);
			//get day of the week 0=Sunday 6=Saturday
			let week = today.getDay();
			//check if it's a holiday (holiday name if it is, undefined if not)
			let is_holiday = JapaneseHolidays.isHoliday(today);
			//if it's neither a weekend nor a holiday
			if(week != 0 && week != 6 && !is_holiday){
				--count;
			}
		}
		//add 1 if it's negative
		++count;
	}
	return count;
}

weekdays(new Date('2023-10-01')); // -8 (if today is 2023-10-11)
weekdays(new Date('2023-10-21')); // 8 (if today is 2023-10-11)

In this code, if the input is a past date, the weekday count will be something like -1.

Similarly, to count only weekends/holidays, change the `if` statements on lines 15 and 28 as follows.

//if it's a weekend or a holiday
if(week == 0 || week == 6 || is_holiday){

Comments

We also welcome reports such as 'It worked!'

  • Anonymous

    Thank you for publishing such an article for free.

    I'm so grateful for the easy-to-understand explanation for web beginners!

    I implemented the sample that allows selecting a date 'X business days later', excluding weekends/holidays and specific dates, by copy-pasting. How can I change the display format to [YYYY年MM月DD日]?

    It would be great if I could also display the day of the week. If possible, I would appreciate it if you could advise me.

  • Owner

    To Anonymous #001.

    Perhaps this comment is for this article, not this one...?

    I've added a postscript to here, so please check it.

After reviewing the content, we will publish it, omitting personal information.

Enter your name and email address as well

Please enter if you would like a reply by email.

The personal information provided will not be disclosed. It will only be used for replies.

It will be sent directly. Please confirm and click 'Send'.

If this was helpful, we appreciate your support!
Any support received will be used for childcare.

Support on OFUSE


Or support us by buying something from the buttons below
(You don't have to buy the linked product.)

Amazon

楽天市場

Yahoo!ショッピング

PR

As an Amazon Associate, 'Ken' earns from qualifying purchases.

Share

Share on Twitter Share on Facebook Share on LINE Share on Hatena Bookmark