Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
moment-business-days
Advanced tools
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.
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'));
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 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 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.
This is a momentJS plugin that allows you to use only business days (Monday to Friday).
NOTES:
var moment = require('moment-business-days');
// You'll be able use moment as you normally do
var moment = require('moment-business-days');
var july4th = '07-04-2015';
var laborDay = '09-07-2015';
moment.locale('us', {
holidays: [july4th, laborDay],
holidayFormat: 'MM-DD-YYYY'
});
// moment-business-days will now stop considering these holidays as business days
var moment = require('moment-business-days');
moment.locale('us', {
workingWeekdays: [1,2,3,4,5,6]
});
// Specifies days form 1 to 6 as a workingday, thus monday to saturday
// When ommiting this configuration parameter, workingdays as used based on locale default
npm test
businessAdd(days)
Will add just business days excluding Saturday and Sunday, return a moment date object:
// 30-01-2015 is Friday, DD-MM-YYYY is the format
moment('30-01-2015', 'DD-MM-YYYY').businessAdd(3)._d // Wed Feb 04 2015 00:00:00 GMT-0600 (CST)
businessSubtract(days)
Will subtract just business days excluding Saturday and Sunday, return a moment date object:
// 27-01-2015 is Tuesday, DD-MM-YYYY is the format
moment('27-01-2015', 'DD-MM-YYYY').businessSubtract(3)._d // Thu Jan 22 2015 00:00:00 GMT-0600 (CST)
isBusinessDay()
Check if the date is a business day and return true/false:
// 31-01-2015 is Saturday
moment('31-01-2015', 'DD-MM-YYYY').isBusinessDay() // false
// 30-01-2015 is Fridat
moment('30-01-2015', 'DD-MM-YYYY').isBusinessDay() // true
nextBusinessDay()
Will retrieve the next business date as moment date object:
//Next busines day of Friday 30-01-2015
moment('30-01-2015', 'DD-MM-YYYY').nextBusinessDay()._d // Mon Feb 02 2015 00:00:00 GMT-0600 (CST)
//Next busines day of Monday 02-02-2015
moment('02-02-2015', 'DD-MM-YYYY').nextBusinessDay()._d //Tue Feb 03 2015 00:00:00 GMT-0600 (CST)
prevBusinessDay()
Will retrieve the previous business date as moment date object:
//Previous busines day of Monday 02-02-2015
moment('02-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d // Fri Jan 30 2015 00:00:00 GMT-0600 (CST)
//Previous busines day of Tuesday 03-02-2015
moment('03-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d //Mon Feb 02 2015 00:00:00 GMT-0600 (CST)
monthBusinessDays()
Retrieve an array of the business days in the month, each one is a moment object.
//Busines days in month January 2015
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessDays()
/*
[ { _isAMomentObject: true,
_i: '01-01-2015',
_f: 'DD-MM-YYYY',
_isUTC: false,
_pf:{ ... },
_locale: { ... },
_d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST)
} {
...
},
( ... )
]
*/
monthNaturalDays()
Is like monthBusinessDays(), but this method will include the weekends on it's response.
monthBusinessWeeks()
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 on the middle of the week, for example: the first week of January 2015 just have two days, Thursday 1st and Friday 2nd. Each day in the week arrays are moment objects.
//Busines weeks in month January 2015
moment('01-01-2015', 'DD-MM-YYYY').monthBusinessWeeks()
/*
[ [ { _isAMomentObject: true,
_i: '01-01-2015',
_f: 'DD-MM-YYYY',
_isUTC: false,
_pf: [...],
_locale: [...],
_d: Thu Jan 01 2015 00:00:00 GMT-0600 (CST)
}, { _isAMomentObject: true,
_i: '01-01-2015',
_f: 'DD-MM-YYYY',
_isUTC: false,
_pf: [...],
_locale: [...],
_d: Fri Jan 02 2015 00:00:00 GMT-0600 (CST) }
],
[...]
]
*/
monthNaturalWeeks()
It's like monthBusinessWeeks(), but this method will include weekends on it's response.
The objects returned by functions are momentjs objects (except isBusinessDay) so you can handle it with moment native functions.
FAQs
MomentJS plugin to use business days
The npm package moment-business-days receives a total of 63,458 weekly downloads. As such, moment-business-days popularity was classified as popular.
We found that moment-business-days demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.