Skip to content

Instantly share code, notes, and snippets.

@ioleo
Last active September 8, 2017 12:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save ioleo/6dd2487999c31527ee2e to your computer and use it in GitHub Desktop.
Save ioleo/6dd2487999c31527ee2e to your computer and use it in GitHub Desktop.
introduce 'workdays' mode to moment.js add/subtract methods (PL national holidays)
(function (undefined) {
/**
* moment.easter
* Source: https://github.com/zaygraveyard/moment-easter
* License: MIT
*/
moment.easter = function Easter20ops(year) {
var a = (year / 100 | 0) * 1483 - (year / 400 | 0) * 2225 + 2613;
var b = ((year % 19 * 3510 + (a / 25 | 0) * 319) / 330 | 0) % 29;
var c = 148 - b - ((year * 5 / 4 | 0) + a - b) % 7;
return moment({year: year, month: (c / 31 | 0) - 1, day: c % 31 + 1});
};
moment.fn.easter = function () {
return moment.easter(this.year());
};
/**
* moment.isWorkday
* Comment: These are National Holidays for POLAND.
* License: MIT
*/
moment.fn.isWorkday = function() {
var easter = moment().easter(this.year());
/* Exclude constrant holidays */
return !(this.isoWeekday() === 6) // exclude Saturday (Weekend)
&& !(this.isoWeekday() === 7) // exclude Sunday (Weekend)
&& !(this.dayOfYear() === 1) // exclude New Year's Day (National holiday)
&& !(this.dayOfYear() === 6) // exclude Epiphany (National holiday)
&& !(this.date() === 1 && this.month() === 4) // exclude Labor Day (National holiday)
&& !(this.date() === 3 && this.month() === 4) // exclude Constitution Day (National holiday)
&& !(this.date() === 15 && this.month() === 7) // exclude Army Day (National holiday)
&& !(this.date() === 1 && this.month() === 10) // exclude All Saints' Day (National holiday)
&& !(this.date() === 11 && this.month() === 10) // exclude Independence Day (National holiday)
&& !(this.date() === 25 && this.month() === 11) // exclude Christmas First Day (National holiday)
&& !(this.date() === 26 && this.month() === 11) // exclude Christmas Second Day (National holiday)
/* Exclude moveing holidays */
// no need to exclude Easter Day - it's always on Sunday
&& !(this.isSame(easter.clone().add(1, 'days'))) // exclude Easter Monday (National holiday)
// no need to exclude Whit Sunday - it's always on Sunday
&& !(this.isSame(easter.clone().add(60, 'days'))) // exclude Corpus Christi (National holiday)
;
};
/**
* moment.add/subtract workdays
* Comment: Uses moment.isWorkday to determine if
* License: MIT
*/
var oldAdd = moment.fn.add;
var oldSubtract = moment.fn.subtract;
moment.fn.add = function (input, val) {
if (val === 'workdays') {
var increment = input / Math.abs(input);
var date = this.clone().add(Math.floor(Math.abs(input) / 5) * 7 * increment, 'days');
var remaining = input % 5;
while(remaining != 0) {
date.add(increment, 'days');
if(date.isWorkday()) {
remaining -= increment;
}
}
return date;
}
return oldAdd.call(this, input, val);
};
moment.fn.subtract = function (input, val) {
if (val === 'workdays') {
var decrement = input / Math.abs(input);
var date = this.clone().subtract(Math.floor(Math.abs(input) / 5) * 7 * decrement, 'days');
var remaining = input % 5;
while(remaining != 0) {
date.subtract(decrement, 'days');
if(date.isWorkday()) {
remaining -= decrement;
}
}
return date;
}
return oldSubtract.call(this, input, val);
};
}).call(this);
@bumbico
Copy link

bumbico commented Mar 21, 2016

How exactly do you use this? I assumed it altered how .add() and .subtract() worked, but it didn't do anything.

@ryandagg
Copy link

Which country is this for?

@edouard-lopez
Copy link

@ryandagg the comment says Poland:

 * Comment: These are National Holidays for POLAND.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment