Last active
January 19, 2023 03:12
-
-
Save jrhames/5200024 to your computer and use it in GitHub Desktop.
Holidays plugin for Moment.JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## Moment.JS Holiday Plugin | |
// | |
//Usage: | |
// Call .holiday() from any moment object. If date is a US Federal Holiday, name of the holiday will be returned. | |
// Otherwise, return nothing. | |
// | |
// Example: | |
// `moment('12/25/2013').holiday()` will return "Christmas Day" | |
// | |
//Holidays: | |
// You can configure holiday bellow. The 'M' stands for Month and represents fixed day holidays. | |
// The 'W' stands for Week, and represents holidays with date based on week day rules. | |
// Example: '10/2/1' Columbus Day (Second monday of october). | |
// | |
//License: | |
// Copyright (c) 2013 [Jr. Hames](http://jrham.es) under [MIT License](http://opensource.org/licenses/MIT) | |
(function() { | |
var moment; | |
moment = typeof require !== "undefined" && require !== null ? require("moment") : this.moment; | |
//Holiday definitions | |
var _holidays = { | |
'M': {//Month, Day | |
'01/01': "New Year's Day", | |
'07/04': "Independence Day", | |
'11/11': "Veteran's Day", | |
'11/28': "Thanksgiving Day", | |
'11/29': "Day after Thanksgiving", | |
'12/24': "Christmas Eve", | |
'12/25': "Christmas Day", | |
'12/31': "New Year's Eve" | |
}, | |
'W': {//Month, Week of Month, Day of Week | |
'1/3/1': "Martin Luther King Jr. Day", | |
'2/3/1': "Washington's Birthday", | |
'5/5/1': "Memorial Day", | |
'9/1/1': "Labor Day", | |
'10/2/1': "Columbus Day", | |
'11/4/4': "Thanksgiving Day" | |
} | |
}; | |
moment.fn.holiday = function() { | |
var diff = 1+ (0 | (this._d.getDate() - 1) / 7), | |
memorial = (this._d.getDay() === 1 && (this._d.getDate() + 7) > 30) ? "5" : null; | |
return (_holidays['M'][this.format('MM/DD')] || _holidays['W'][this.format('M/'+ (memorial || diff) +'/d')]); | |
}; | |
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) { | |
module.exports = moment; | |
} | |
}(this)); |
I want to get the full list of the national holidays (names) how can I use this?
@kodie, I tried to use your plugin on my Angular 2 app and I couldn’t get it to work. Any idea why? I got it to work on my Hugo project and it’s great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've created a plugin for moment that takes care of all your holiday handling and calculating needs: https://github.com/kodie/moment-holiday
My
isHoliday()
function accomplishes exactly what @jrhames's gist was attempting to do accurately (@baburdick pointed out that this gist doesn't accurately figure out when Thanksgiving or Memorial day is)For example:
Also, my
holiday()
function does exactly what @ihomecall was needing.For example:
Let me know what you think!