Published: February 03 2024

JavaScript - Add Business Days to Date (exclude Holidays & Weekends)

The below code sample shows how to add business days to a date, excluding weekends and public holidays in any specified country and state/region. I use it to calculate delivery dates for purchased products in an ecommerce website.

At first I was looking into APIs that perform date calculation or return public holiday dates for a given location and year, but then I found this little npm package called date-holidays that does everything I need for free and without any lag.

The date-holidays package includes the isHoliday(date) method to check if the specified date is a holiday for any given country and (optional) state/region. The isHoliday method handles pretty much all the heavy lifting, I just add a check for weekends to determine if the date is a business day.

import Holidays from 'date-holidays';

export function addBusinessDays(country, state, startDate, daysToAdd) {
    const holidays = new Holidays(country, state);
    const isHoliday = (date) => holidays.isHoliday(date);

    const weekend = { 6: 'saturday', 0: 'sunday' };
    const isWeekend = (date) => !!weekend[date.getDay()];

    // start from startDate param
    const date = new Date(startDate);

    // add days until daysToAdd reaches zero
    while (daysToAdd > 0) {
        date.setDate(date.getDate() + 1);

        const isBusinessDay = !isHoliday(date) && !isWeekend(date);

        // only decrement for business days
        if (isBusinessDay) {
            daysToAdd -= 1;
        }
    }

    return date;
}


Interactive Demo

Test out the addBusinessDays function below, there's a form field for each function parameter (country, state, startDate, daysToAdd).

A list of supported countries and regions can be found here.

(See on StackBlitz at https://stackblitz.com/edit/js-add-business-days)


Demo Code

This is the Vanilla JS code that powers the form in the interactive demo. There's no much to it, it sets some initial form values and calls addBusinessDays(...) on button click.

import './style.css';

import { addBusinessDays } from './addBusinessDays';

// get form elements
const country = document.querySelector('#country');
const state = document.querySelector('#state');
const startDate = document.querySelector('#start-date');
const daysToAdd = document.querySelector('#days-to-add');
const button = document.querySelector('#calculate');
const result = document.querySelector('#result');

// set initial values
country.value = 'AU';
state.value = 'NSW';
startDate.value = new Date().toISOString().slice(0, 10);
daysToAdd.value = 1;

// calculate date on button click
button.onclick = () => {
    try {
        const resultDate = addBusinessDays(
            country.value,
            state.value,
            startDate.value,
            daysToAdd.value
        );
        result.innerText = resultDate.toISOString().slice(0, 10);
    } catch (e) {
        result.innerText = e.message;
    }

    return false;
};

 


Need Some JavaScript Help?

Search fiverr for freelance JavaScript developers.


Follow me for updates

On Twitter or RSS.


When I'm not coding...

Me and Tina are on a motorcycle adventure around Australia.
Come along for the ride!


Comments


Supported by