Counting working days with Moment.js
A few months ago I published a plugin for Moment.js that allows you to calculate: how many N working days are from today in calendar days? What date will be after N business days from the given date? how many working days in a given range? The ability to configure working days and exceptions in the form of holidays is available.
The plugin can be found on github: https://github.com/andruhon/moment-weekday-calc
The plugin can be installed via bower and npm:
The plugin adds several functions to Moment.js:
Each of the functions is available with the iso prefix, such functions use many working days starting from Monday (1-7), functions without a prefix use the American format starting from Sunday (0-6).
There are many options for calling these functions, both with a list of arguments, and with an object with named parameters, I won’t list them, but I will immediately turn to examples.
Usage:
I give examples only for functions with the prefix iso.
How many Fridays between February 14 and 23?
How many working days, excluding holidays, from April 1, 2015 to March 31, 2016?
And if you take into account a couple of holidays?
Call with object:
What kind of date will be 5 working days after February 2 if you work seven days a week?
5 working days after May 4, taking into account May 9?
11 working days after October 10 in calendar days, working days - Wednesday-Sunday:
Read more in README on the github.
Thanks in advance for sound criticism. I hope that the plugin will be useful to someone.
The plugin can be found on github: https://github.com/andruhon/moment-weekday-calc
The plugin can be installed via bower and npm:
bower install moment-weekday-calc
npm install moment-weekday-calc
The plugin adds several functions to Moment.js:
- int weekdayCalc - counts how many "working" days in a given range
- date addWorkdays - Finds the date after N "working" (Mon-Fri) days
- int workdaysToCalendarDays - converts workdays to calendar
- date addWeekdaysFromSet - adds days from a given set to a given date
- int weekdaysFromSetToCalendarDays - converts days from a given set to calendar days
Each of the functions is available with the iso prefix, such functions use many working days starting from Monday (1-7), functions without a prefix use the American format starting from Sunday (0-6).
There are many options for calling these functions, both with a list of arguments, and with an object with named parameters, I won’t list them, but I will immediately turn to examples.
Usage:
I give examples only for functions with the prefix iso.
How many Fridays between February 14 and 23?
moment('14 Feb 2014').isoWeekdayCalc('23 Feb 2014',[5]); //2
(in this case, the beginning of the range is taken from the moment object, from which we call the function)How many working days, excluding holidays, from April 1, 2015 to March 31, 2016?
moment().isoWeekdayCalc('1 Apr 2015','31 Mar 2016',[1,2,3,4,5]); //262
(here the moment object does not contain a date, so the start date is specified as the first argument)And if you take into account a couple of holidays?
moment().isoWeekdayCalc('1 Apr 2015','31 Mar 2016',[1,2,3,4,5],['6 Apr 2015','7 Apr 2015']); //260
Call with object:
moment().isoWeekdayCalc({
rangeStart: '1 Apr 2015',
rangeEnd: '31 Mar 2016',
weekdays: [1,2,3,4,5],
exclusions: ['6 Apr 2015','7 Apr 2015']
}) //260
What kind of date will be 5 working days after February 2 if you work seven days a week?
moment('2015-02-02').isoAddWeekdaysFromSet(5, [1,2,3,4,5,7]); //2015-02-08
5 working days after May 4, taking into account May 9?
moment('2015-05-04').isoAddWeekdaysFromSet({
'workdays': 5,
'weekdays': [1,2,3,4,5,6],
'exclusions': ['2015-05-09']
}); //2015-05-11
11 working days after October 10 in calendar days, working days - Wednesday-Sunday:
moment('2015-10-05').isoWeekdaysFromSetToCalendarDays(11, [3,4,5,6,7], ['2015-10-15']) //17
Read more in README on the github.
Thanks in advance for sound criticism. I hope that the plugin will be useful to someone.