Day.js plugin to add support for calculating dates only accounting for Business days
npm install dayjs-business-days2
This is a plugin for Day.js that allows for Date calculations to take place that only consider Business Days i.e Monday to Friday
- Calculate if date is a Business Day
- Add/Subtract Business Days
- Calculate difference between dates in Business Days
- Get Next/Prev Business Day
- Get all Business Days/Weeks in a Month
- Get the last Business Day of a month
- Based off of moment-business-days
- Forked from dayjs-business-days and converted to TypeScript
 
- dayjs-business-days2
- Current CI/CD Status
- Contents
- Getting Started
- Prerequisites
- Installing
- Usage Guide
- isHoliday() =\> Boolean
- isBusinessDay() =\> Boolean
- isAdditionalWorkingDay() =\> Boolean
- businessDaysAdd(number) =\> Day.js Object
- businessDaysSubtract(number) =\> Day.js Object
- businessDiff(date) =\> Number
- nextBusinessDay() =\> Day.js Object
- prevBusinessDay() =\> Day.js Object
- [businessDaysInMonth() =\> \[Day.js Object\]](#businessdaysinmonth--dayjs-object)
- lastBusinessDayOfMonth() =\> Day.js Object
- [businessWeeksInMonth() =\> \[\[Day.js Object\]\]](#businessweeksinmonth--dayjs-object)
- [getWorkingWeekdays() =\> \[number\]](#getworkingweekdays--number)
- setWorkingWeekdays() =\> void
- [getHolidays() =\> \[string\]](#getholidays--string)
- setHolidays() =\> void
- getHolidayFormat() =\> string
- setHolidayFormat() =\> void
- [getAdditionalWorkingDays() =\> \[string\]](#getadditionalworkingdays--string)
- setAdditionalWorkingDays() =\> void
- getAdditionalWorkingDayFormat() =\> string
- setAdditionalWorkingDayFormat() =\> void
- Local Development and Contributing
The following guide will help you use the plugin with Day.js, and explain the plugins API.
Day.js version 1.x installed
You can install via Yarn or npm
``bash`
yarn add dayjs-business-days2
`bash`
npm install dayjs-business-days2
You will need to import the plugin and activate it via the Day.js .extend() function
`javascript
import dayjs from 'dayjs';
import dayjsBusinessDays from 'dayjs-business-days2';
dayjs.extend(dayjsBusinessDays);
`
Check if the date is a holiday. Returns true or false
`javascript2020-12-25
// Add holidays to plugin options
const options = {
holidays: [],YYYY-MM-DD
holidayFormat: ,
};
dayjs.extend(businessDays, options);
// Christmas day is a Friday
dayjs(2020-12-25).isHoliday(); // returns true`
Check if the date is a business day. Returns true or false
`javascript2020-12-25
// Christmas day is a Friday
dayjs().isBusinessDay(); // returns true
// Boxing day is a Saturday
dayjs(2020-12-26).isBusinessDay(); // returns false`
Check if the date is an additional working day. Returns true or false
`javascript2023-01-28
// Add holidays to plugin options
const options = {
additionalWorkingDays: [],YYYY-MM-DD
additionalWorkingDayFormat: ,
};
dayjs.extend(businessDays, options);
// Check
dayjs(2023-01-28).isAdditionalWorkingDay(); // returns true`
- number {Number} Number of Business Days to be added
Adds the number of Business Days to the current date. Returns the new date as a Day.js object
`javascript2020-12-25
dayjs().businessDaysAdd(3).format(DD/MM/YYYY); // returns 30/12/2020`
- number {Number} Number of Business Days to be subtracted
Subtracts the number of Business Days from the current date. Returns the new date as a Day.js object
`javascript2020-12-30
dayjs().businessDaysSubtract(3).format(DD/MM/YYYY); // returns 25/12/2020`
- date {Day.js Date} The date to be diffed from
Calculates the number of business days between a Day.js date and date. Returns the difference as a positive or negative number.
`javascript2020-12-25
dayjs().businessDiff(dayjs(2020-12-30)); // returns -52020-12-30
dayjs().businessDiff(dayjs(2020-12-25)); // returns 5`
Calculates the next Business Day. Returns a Day.js object
`javascript2020-12-25
// 25th December 2020 is a Friday. Next business day is Monday 28th December.
dayjs().nextBusinessDay().format(DD/MM/YYYY); // returns 28/12/2020`
Calculates the previous Business Day. Returns a Day.js object
`javascript2020-12-28
// 28th December 2020 is a Monday. Previous business day is Friday 25th December.
dayjs().prevBusinessDay().format(DD/MM/YYYY); // returns 25/12/2020`
Calculates all of the business days in a given month. Returns an array of Day.js objects
`javascript2020-12-25
dayjs().businessDaysInMonth();2020-12-01
// returns equivalent of [dayjs(), dayjs(2020-12-02), ...]`
Calculates the last Business Day of the month. Returns a Day.js object
`javascript2021-09-01
// 30th September 2021 is a Thursday and is the last business day of the month.
dayjs().lastBusinessDayOfMonth().format(DD/MM/YYYY); // returns 30/09/2021`
Calculates all of the business weeks and days in a given month. Returns an two dimensional array of Day.js objects
`javascript2020-12-25
dayjs().businessWeeksInMonth();2020-12-01
// returns equivalent of
// [
// [dayjs(), dayjs(2020-12-02), ...],2020-12-07
// [dayjs(), dayjs(2020-12-08), ...],`
// ...
// ]
Returns an array of numbers representing the current days fo the week that are considered business days
`javascript`
dayjs.getWorkingWeekdays(); // returns [ 1, 2, 3, 4, 5 ]
Sets the days of the week that are considered business days to the given array of numbers where 0 is Sunday and 6 is Saturday
`javascript`
dayjs.setWorkingWeekdays([ 0, 3, 4, 5 ]);
Returns an array of strings representing the currently set holidays
`javascript2020-12-25
const options = {
holidays: [],YYYY-MM-DD
holidayFormat: ,
};
dayjs.extend(businessDays, options);
dayjs.getHolidays(); // returns [ 2020-12-25 ]`
Sets the holiday list to the given array of strings
`javascript2020-12-25
dayjs.setHolidays([ , 2021-01-01 ]);`
Returns a string representing the currently expected holiday format
`javascript2020-12-25
const options = {
holidays: [],YYYY-MM-DD
holidayFormat: ,
};
dayjs.extend(businessDays, options);
dayjs.getHolidayFormat(); // returns YYYY-MM-DD`
Sets the holiday list to the given a string
`javascriptMM-DD-YYYY
dayjs.setHolidayFormat();`
Returns an array of strings representing the currently set additional working days
`javascript2023-01-28
const options = {
additionalWorkingDays: [],YYYY-MM-DD
additionalWorkingDayFormat: ,
};
dayjs.extend(businessDays, options);
dayjs.getAdditionalWorkingDays(); // returns [ 2023-01-28 ]`
Sets the additional working day list to the given array of strings
Additional working days are days that your business is open outside of your regular working weekdays.
`javascript2023-01-28
dayjs.setAdditionalWorkingDays([ , 2023-01-29 ]);`
Returns a string representing the currently expected additional working day format
`javascript2023-01-28
const options = {
additionalWorkingDays: [],YYYY-MM-DD
additionalWorkingDayFormat: ,
};
dayjs.extend(businessDays, options);
dayjs.getAdditionalWorkingDayFormat(); // returns YYYY-MM-DD`
Sets the additional working day format to the given a string
`javascriptMM-DD-YYYY
dayjs.setAdditionalWorkingDayFormat();`
I am more than happy to accept PRs for bugs, improvements or new features.
Developing your own changes locally is easy, you just need to clone the repo
`bash
git clone git@github.com/reediculous456/dayjs-business-days
cd dayjs-business-days
`
and install the dependencies with either npm or yarn
`bash`
npm i
`bash`
yarn
Tests can be ran with the test script
`bash`
npm run test
`bash``
yarn test