dayjs-business-time
A Day.js plugin that allows you to work with Business Time.
- Business Days
- Business Hours
- Business Minutes
- Customize business days and hours
- Customize Holidays to prevent them to be counted as Business Days
Getting Started
Special Version
Table of Contents
Instalation
With NPM
npm i dayjs-business-time
With Yarn
yarn add dayjs-business-time
Usage
NodeJS
const dayjs = require('dayjs');
const dayjsBusinessTime = require('dayjs-business-time');
dayjs.extend(dayjsBusinessTime);
Typescript
import dayjs from 'dayjs';
import dayjsBusinessTime from 'dayjs-business-time';
dayjs.extend(dayjsBusinessTime);
Setup
Setting Holidays
By default, holidays are empty!
const holidays: string[] = [
'2021-01-01',
'2021-01-25',
'2021-06-03',
];
dayjs.setHolidays(holidays);
Getting Holidays
const holidays: string[] = dayjs.setHolidays(holidays);
console.log(holidays);
Setting Business Times
By default, Business Times are Monday-Friday, 9am - 5pm, but you can setup as many Business Segments you want in a day
const businessTimes: BusinessHoursMap = {
sunday: null,
monday: [
{ start: '09:00:00', end: '17:00:00' }
],
tuesday: [
{ start: '09:00:00', end: '12:00:00' },
{ start: '13:00:00', end: '18:00:00' }
],
wednesday: [
{ start: '09:00:00', end: '12:00:00' },
{ start: '13:00:00', end: '16:00:00' },
{ start: '13:00:00', end: '17:00:00' }
],
thursday: [
{ start: '09:00:00', end: '17:00:00' }
],
friday: [
{ start: '09:00:00', end: '17:00:00' }
],
saturday: null,
}
dayjs.setBusinessTime(businessTimes);
Checking
Check if a date is a Holiday
According to holidays setup
const isHoliday = dayjs('2021-02-01').isHoliday();
console.log(isHoliday);
Check if a date is a Business Day
Bussiness Days are days with Business Hours settled, excluding Holidays.
const isBusinessDay = dayjs('2021-02-01').isBusinessDay();
console.log(isBusinessDay);
Check if a Time is Business Time
Bussiness Times are all minutes between Start and End of a Business Time Segment.
const isBusinessTime = dayjs('2021-02-01 10:00:00').isBusinessTime();
console.log(isBusinessTime);
Next and Last
Get Next Business Day
const nextBusinessDay = dayjs('2021-02-01').nextBusinessDay();
console.log(nextBusinessDay);
Get Last Business Day
const lastBusinessDay = dayjs('2021-02-01').lastBusinessDay();
console.log(nextBusinessDay);
Get Next Business Time
const nextBusinessTime = dayjs('2021-02-01 18:00:00').nextBusinessTime();
console.log(nextBusinessTime);
Get Last Business Time
const lastBusinessTime = dayjs('2021-02-01 08:00:00').lastBusinessTime();
console.log(lastBusinessTime);
Adding Business Time
Add Business Time
const day = dayjs('2021-02-01 10:00:00');
const timeToAdd: number = 2;
const unit: BusinessTimeUnit = 'days';
const newBusinessTime: Dayjs = day.addBusinessTime(timeToAdd, unit);
console.log(newBusinessTime);
Add Business Days
This method is just an alias for .addBusinessTime(timeToAdd, 'days')
const day = dayjs('2021-02-01 10:00:00');
const timeToAdd: number = 2;
const newBusinessTime: Dayjs = day.addBusinessDays(timeToAdd);
console.log(newBusinessTime);
Add Business Hours
This method is just an alias for .addBusinessTime(timeToAdd, 'hours')
const day = dayjs('2021-02-01 10:00:00');
const timeToAdd: number = 2;
const newBusinessTime: Dayjs = day.addBusinessHours(timeToAdd);
console.log(newBusinessTime);
Add Business Minutes
This method is just an alias for .addBusinessTime(timeToAdd, 'minutes')
const day = dayjs('2021-02-01 10:00:00');
const timeToAdd: number = 30;
const newBusinessTime: Dayjs = day.addBusinessMinutes(timeToAdd);
console.log(newBusinessTime);
Subtracting Business Time
Subtract Business Time
const day = dayjs('2021-02-01 10:00:00');
const timeToSubtract: number = 2;
const unit: BusinessTimeUnit = 'days';
const newBusinessTime: Dayjs = day.subtractBusinessTime(timeToSubtract, unit);
console.log(newBusinessTime);
Subtract Business Days
This method is just an alias for .subtractBusinessTime(timeToSubtract, 'days')
const day = dayjs('2021-02-01 10:00:00');
const timeToSubtract: number = 2;
const newBusinessTime: Dayjs = day.subtractBusinessDays(timeToSubtract);
console.log(newBusinessTime);
Subtract Business Hours
This method is just an alias for .subtractBusinessTime(timeToSubtract, 'hours')
const day = dayjs('2021-02-01 12:00:00');
const timeToSubtract: number = 2;
const newBusinessTime: Dayjs = day.subtractBusinessHours(timeToSubtract);
console.log(newBusinessTime);
Subtract Business Minutes
This method is just an alias for .subtractBusinessTime(timeToSubtract, 'minutes')
const day = dayjs('2021-02-01 10:00:00');
const timeToSubtract: number = 30;
const newBusinessTime: Dayjs = day.subtractBusinessMinutes(timeToSubtract);
console.log(newBusinessTime);
Diff
Business Time Diff
const start: Dayjs = dayjs('2021-02-01 10:00:00');
const end: Dayjs = dayjs('2021-02-04 10:00:00');
const unit: BusinessTimeUnit = 'days';
const difference: number = start.businessTimeDiff(end, unit);
console.log(difference);
Business Days Diff
This method is just an alias for .businessTimeDiff(dateToCompare, 'days')
const start: Dayjs = dayjs('2021-02-01 10:00:00');
const end: Dayjs = dayjs('2021-02-04 10:00:00');
const difference: number = start.businessDaysDiff(end);
console.log(difference);
Business Hours Diff
This method is just an alias for .businessTimeDiff(dateToCompare, 'hours')
const start: Dayjs = dayjs('2021-02-01 10:00:00');
const end: Dayjs = dayjs('2021-02-01 15:00:00');
const difference: number = start.businessHoursDiff(end);
console.log(difference);
Business Minutes Diff
This method is just an alias for .businessTimeDiff(dateToCompare, 'minutes')
const start: Dayjs = dayjs('2021-02-01 10:00:00');
const end: Dayjs = dayjs('2021-02-01 10:45:00');
const difference: number = start.businessMinutesDiff(end);
console.log(difference);