npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

business-days-calculator

v2.2.0

Published

Library for calculating the number of working days (business days) between two dates (considering a Holiday Calendar given).

Downloads

489

Readme

BusinessDaysCalculator

This is a library for calculating the number of business days between two dates, considering the number of holidays in the range.

You can use it as a Node.js module or in browser.

Instalation

If you use NPM, then you can install it with npm install business-days-calculator. Otherwise, you can download the latest release and use it in your browser or in other Javascript-based projects.

Usage

Node.js

var calculator = require("business-days-calculator");
var calendar = require("holidays-calendar-brazil");
calculator.SetCalendar(calendar);

var today = new Date();
console.log('Today '+ (calculator.IsBusinessDay(today) ? 'is' : 'isn\'t') +' a business day');
console.log('Today '+ (calculator.IsHoliday(today) ? 'is' : 'isn\'t') +' a holiday');
console.log('The Carnival next year is due to '+ (calculator.NextHoliday(new Date("1 January "+today.getFullYear()))));

Browser

<script src="../node_modules/holidays-calendar/dist/HolidaysCalendar.js"></script>
<script src="../node_modules/holidays-calendar-brazil/src/HolidaysCalendar-brazil.js" charset="utf-8"></script>
<script src="../dist/BusinessDaysCalculator.js"></script>
<script>
var today = new Date();
console.log('Today '+ (BusinessDaysCalculator.IsBusinessDay(today) ? 'is' : 'isn\'t') +' a business day');
console.log('Today '+ (BusinessDaysCalculator.IsHoliday(today) ? 'is' : 'isn\'t') +' a holiday');
console.log('The Carnival next year is due to '+ (BusinessDaysCalculator.NextHoliday(new Date("1 January "+today.getFullYear()))));
</script>

API Reference

ContinuousDaysBetween(date1, date2)

Counts the number of continuous/consecutive days between date1 and date2

date1: date, required. Starting date.

date2: date, required. Ending date.

return: integer. Number of continous days between dates.

Example

var calculator = require('business-days-calculator');
 
var data = calculator.ContinuousDaysBetween(new Date("1 January 2020"), new Date("31 January 2020"));
\\data equals to 30

HolidaysBetween(date1, date2)

Counts the number of holidays between date1 and date2

date1: date, required. Starting date.

date2: date, required. Ending date.

return: integer. Number of holidays according to the calendar in use.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.HolidaysBetween(new Date("1 January 2020"), new Date("31 December 2020"));
\\data equals to 12

IsBusinessDay(date)

Verifies if date doesn't correspond to a Sunday or Saturday, neither is a holiday.

date: date, required. Date reference.

return: Boolean. Whether date is a business day or not.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.IsBusinessDay(new Date("1 January 2020"));
\\data equals to false

IsHoliday(date)

Verifies if date happens to be a holiday.

date: date, required. Date reference.

return: Boolean. Whether date is a holiday or not.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.IsHoliday(new Date("1 January 2020"));
\\data equals to true

Locale()

Verifies which locale/calendar the library is set to use.

return: String. Locale identificator.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.Locale();
\\data equals to 'brazil'

NextHoliday(date)

Retrieves the next holiday after date.

date: date, required. Date reference (date won't be considered as a candidate).

return: Mixed. date if there's a holiday in the calendar anywhere after date; false, otherwise.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.NextHoliday(new Date("1 January 2020"));
\\data equals to 'Mon Feb 24 2020' (which is Carnival)

NextWorkingDay(date, considerHolidays)

Retrieves the next business/working day after date.

date: date, required. Date reference (date won't be considered as a candidate).

considerHolidays: boolean, optional (default: true). Whether the holiday calendar should be used in the filter. If negative, this method will output the first week day after date;

return: date. Next business/working day.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.NextHoliday(new Date("1 January 2020"));
\\data equals to 'Mon Feb 24 2020' (which is Carnival)

SetCalendar(calendar)

Configures the library to use calendar as a reference for holidays.

calendar: Mixed, required. object, as an extension of 'holidays-calendar' library (useful in Node.js environment only); String representing the locale to be used.

Example

var brazilianCalendar = require('holidays-calendar-brazil');
var calendar = require('holidays-calendar');
var calculator = require('business-days-calculator');

calculator.SetCalendar(brazilianCalendar);

var data = calculator.Locale();
\\data equals to 'brazil'

calendar.AddCalendar('generic',{}); // adds a new calendar, although empty

calculator.SetCalendar(calendar);
data = calculator.Locale();
\\data equals to 'generic'

calendar.AddCalendar('generic2',{}); // adds a new calendar, although empty
calculator.SetCalendar('generic2');
data = calculator.Locale();
\\data equals to 'generic2'

WorkingDaysBetween(date1, date2, discountHolidays)

Counts the number of business days between date1 and date2

date1: date, required. Starting date.

date2: date, required. Ending date.

discountHolidays: boolean, optional (default: true). Whether the library should consider holidays in the counting.

return: integer. Number of business days according to the calendar in use.

Example

var calendar = require('holidays-calendar-brazil');
var calculator = require('business-days-calculator');
calculator.SetCalendar(calendar);
 
var data = calculator.WorkingDaysBetween(new Date("1 January 2020"), new Date("31 December 2020"));
\\data equals to 250
data = calculator.WorkingDaysBetween(new Date("1 January 2020"), new Date("31 December 2020"), false);
\\data equals to 262