moment-biz
Installation
Run npm install moment moment-biz
to install this package and its moment dependency.
Setup
Node.js
Just require the package using:
const moment = ;;
You can also do this with a oneliner:
const moment = moment;
Node loads the bundled country locales automatically when calling the related methods:
const moment = moment; // will load en-US locale automatically if it is not loaded yetmoment;// will load fr-FR locale automatically if it is not loaded yet;
Browsers
You need to add the moment-biz file located in dist in your webpage. It needs to have moment loaded before if you are not using AMD or other module system. (all files in dist/
have an UMD wrapper):
You can also load country locales (specific extension to moment locales that are already configured to support the proper holidays):
You also have a file containing all country locales for ease of use:
Usage
.add() and .subtract() changes
Now both methods supports a businessdays
argument (also works in singular):
const prevBusinessDay = ;const nextBusinessDay = ;const in10BusinessDays = ;const bef10BusinessDays = ;
moment.easter
You also have access to the moment.easter(year)
function that returns you the easter sunday of the provided year.
moment // returns a moment object on April, 16th, 2017
moment.getHolidays
moment.getHolidays(year = moment().year(), locale = moment.locale())
returns the holidays for the current year & locale or the specified ones if set.
moment;// will return a moment object on January, 1st, 2017moment0;
.getHolidaysOfCurrentYear
On any moment instance, you can always call getHolidaysOfCurrentYear()
. It will return the same thing as moment.getHolidays(date.year(), date.locale())
.
const date = ;// will return a moment object on January, 1st, 2017date0;
.isHoliday
You can call date.isHoliday()
to know if the current date is a holiday or not (be careful, country locales tend to contain only holidays that can happen on non weekend days) according to your locale configuration.
; // return true; // return false
.isWeekend
You can call date.isWeekend()
to test if your date is on a weekend according to your locale configuration.
; // return true; // return false
.isFreeDay
You can call date.isFreeDay()
to know if you date is not a business day according to your locale configuration.
; // return true; // return true; // return false
.isBusinessDay
You can call date.isBusinessDay()
to know if you date is a business day according to your locale configuration.
moment;; // return false; // return false; // return true
Country locales
Defining your own country locales
You can easily create country locales. First load the correct main locale, then call moment.defineLocale()
to create it just like this:
moment;moment;
There is 3 main parts to the defineLocale()
call.
First, parentLocale
, defines the basic locale you want to use. Always refer to the main locale you want to fallback to.
Then weekends
defines the week days that are considered weekends in your country. Use 0 = sunday, 6 = saturday
values. It defaults to [0, 6]
on all locales if not provided.
And last, holidays
, that define the proper holidays of your country. It accepts either a DD-MM
date (you can customize this format using the holidaysFormat
configuration variable) that will become a holiday every year or a function that takes a year and return a holiday (or an invalid date if the holiday does not exists the provided year).
Adding country locales to the package itself
I'll gladly accept PRs adding support for new countries. Just add your file & copy paste a test loading a locale to ensure yours keeps loading properly before submitting your PR.