What is moment-business-days?
The moment-business-days npm package extends the Moment.js library to handle business days calculations. It allows you to perform operations like adding or subtracting business days, checking if a date is a business day, and customizing business days and holidays.
What are moment-business-days's main functionalities?
Add Business Days
This feature allows you to add a specified number of business days to a date. In this example, 5 business days are added to October 1, 2023.
const moment = require('moment-business-days');
let date = moment('2023-10-01');
let newDate = date.businessAdd(5);
console.log(newDate.format('YYYY-MM-DD'));
Subtract Business Days
This feature allows you to subtract a specified number of business days from a date. In this example, 3 business days are subtracted from October 10, 2023.
const moment = require('moment-business-days');
let date = moment('2023-10-10');
let newDate = date.businessSubtract(3);
console.log(newDate.format('YYYY-MM-DD'));
Check if a Date is a Business Day
This feature allows you to check if a given date is a business day. In this example, it checks if October 7, 2023, is a business day.
const moment = require('moment-business-days');
let date = moment('2023-10-07');
let isBusinessDay = date.isBusinessDay();
console.log(isBusinessDay);
Custom Business Days and Holidays
This feature allows you to customize the business days and holidays. In this example, it sets the working weekdays to Monday through Friday and adds holidays on December 25, 2023, and January 1, 2023. Then it adds 1 business day to December 24, 2023.
const moment = require('moment-business-days');
moment.updateLocale('us', {
workingWeekdays: [1, 2, 3, 4, 5],
holidays: ['2023-12-25', '2023-01-01'],
holidayFormat: 'YYYY-MM-DD'
});
let date = moment('2023-12-24');
let newDate = date.businessAdd(1);
console.log(newDate.format('YYYY-MM-DD'));
Other packages similar to moment-business-days
date-fns
date-fns is a modern JavaScript date utility library that provides a wide range of functions for manipulating dates, including business day calculations. It is modular and tree-shakeable, making it a lightweight alternative to moment-business-days.
moment-holiday
moment-holiday is another Moment.js extension that allows you to work with holidays and business days. It provides similar functionality to moment-business-days but focuses more on holiday calculations and customization.
js-joda
js-joda is a date and time library for JavaScript that is based on the Java 8 Date and Time API. It provides immutable date and time objects and includes functionality for working with business days and holidays. It is a more modern alternative to Moment.js and its extensions.
moment-business-days
This is a Moment.js plugin that allows you to work with only business days
(Monday to Friday). You can customize the working week, and also set custom dates for holidays to exclude them from
being counted as business days, for example national holidays.
Notes
- This plugin works on both server and client side.
- This plugin is based on momentjs-business.
- All contributions are welcome.
- Thanks to the contributors for making this plugin better!!
Usage
var moment = require('moment-business-days');
<script src="moment.js"></script>
<script src="moment-business-days.js"></script>
Configuration
Use localization to configure holidays
var moment = require('moment-business-days');
var july4th = '07-04-2015';
var laborDay = '09-07-2015';
moment.updateLocale('us', {
holidays: [july4th, laborDay],
holidayFormat: 'MM-DD-YYYY'
});
Use localization to configure business days
var moment = require('moment-business-days');
moment.updateLocale('us', {
workingWeekdays: [1, 2, 3, 4, 5, 6]
});
API
The objects returned by methods are Moment.js objects (except .isBusinessDay()
and .businessDiff()
) so you can
handle them with Moment.js native methods.
.isHoliday()
=> boolean
Check if the date is among the holidays specified, and return true or false:
.isBusinessDay()
=> boolean
Check if the date is a business day, and return true or false:
moment('31-01-2015', 'DD-MM-YYYY').isBusinessDay()
moment('30-01-2015', 'DD-MM-YYYY').isBusinessDay()
.businessDaysIntoMonth()
=> number
Calculate the amount of business days in the month of the Moment.js object.
.businessDiff()
=> number
Calculate the amount of business days between dates.
var diff = moment('05-15-2017', 'MM-DD-YYYY').businessDiff(moment('05-08-2017','MM-DD-YYYY'));
Note that the default behavior of businessDiff
is to return an absolute value,
which is a departure from moment's diff
. To match the behavior of diff
pass
true
as the second argument to businessDiff
:
var diff = moment('05-08-2017', 'MM-DD-YYYY').businessDiff(moment('05-15-2017','MM-DD-YYYY'), true);
.businessAdd(days)
=> Moment
Will add the given number of days skipping non-business days, returning a Moment.js object:
moment('30-01-2015', 'DD-MM-YYYY').businessAdd(3)._d
.businessSubtract(days)
=> Moment
Will subtract the given number of days skipping non-business days, returning a Moment.js object:
moment('27-01-2015', 'DD-MM-YYYY').businessSubtract(3)._d
.nextBusinessDay()
=> Moment
Will retrieve the next business date as a Moment.js object:
moment('30-01-2015', 'DD-MM-YYYY').nextBusinessDay()._d
moment('02-02-2015', 'DD-MM-YYYY').nextBusinessDay()._d
By default only 7 days into the future are checked for the next business day. To search beyond 7 days
set the nextBusinessDayLimit (as a number) higher.
var moment = require('moment-business-days');
moment.updateLocale('us', {
nextBusinessDayLimit: 31
});
.prevBusinessDay()
=> Moment
Will retrieve the previous business date as a Moment.js object:
moment('02-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d
moment('03-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d
By default only the last 7 days are checked for the previous business day. To search beyond 7 days
set the prevBusinessDayLimit (as a number) higher.
var moment = require('moment-business-days');
moment.updateLocale('us', {
prevBusinessDayLimit: 31
});
.monthBusinessDays()
=> Moment[]
Retrieve an array of the business days in the month, each one is a Moment.js object.
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessDays()
.monthNaturalDays()
=> Moment[]
Is like .monthBusinessDays()
, but this method will include the weekends in it's response.
.monthBusinessWeeks()
=> Moment[][]
Retrieve an array of arrays, these arrays are the representation of a business weeks and each week (array) have it own
business days (Monday to Friday). There could be the case that one week (array) have less than 5 days, this is because
the month started in the middle of a week, for example: the first week of January 2015 has just two days,
Thursday 1st and Friday 2nd. Each day in the week arrays are Moment.js objects.
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessWeeks()
.monthNaturalWeeks()
=> Moment[][]
It's like .monthBusinessWeeks()
, but this method will include weekends in it's response.
Installation
// For Node.js
$ npm install moment-business-days
// ...or install and save in package.json
$ npm install --save moment-business-days
// For bower
$ bower install moment-business-days
Testing
npm test