A few months ago, I published a plugin for Moment.js that allows you to calculate: how many are N working days from today in calendar days? What date will be after N working days from the given date? How many working days in a given range? The ability to configure work days and exceptions in the form of holidays is available.
The plugin can be found on github:
https://github.com/andruhon/moment-weekday-calcThe 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 working days to calendar days
- date addWeekdaysFromSet - adds days from the specified set to the specified date
- int weekdaysFromSetToCalendarDays - converts days from a specified multiplicity to calendar days
Each of the functions is available with the iso prefix, such functions use a variety of working days starting on Monday (1-7), functions without prefix use the American format starting on 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 will not list them, but go straight to the examples.
Using:I give examples only for functions with the iso prefix.How many fridays are between February 14 and 23?
moment('14 Feb 2014').isoWeekdayCalc('23 Feb 2014',[5]);
(in this case, the beginning of the range is taken from the moment object, from which we call the function)How many business days excluding holidays from April 1, 2015 to March 31, 2016?
moment().isoWeekdayCalc('1 Apr 2015','31 Mar 2016',[1,2,3,4,5]);
(here the moment object does not contain a date, so the start date is set as the first argument)And if you consider a couple of holidays?
moment().isoWeekdayCalc('1 Apr 2015','31 Mar 2016',[1,2,3,4,5],['6 Apr 2015','7 Apr 2015']);
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'] })
What 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]);
5 working days after May 4, considering May 9?
moment('2015-05-04').isoAddWeekdaysFromSet({ 'workdays': 5, 'weekdays': [1,2,3,4,5,6], 'exclusions': ['2015-05-09'] });
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'])
Read more in the
README on github.
Thanks in advance for sensible criticism. I hope that the plugin will be useful to someone.