moment-holiday
A Moment.js plugin for handling holidays.
Requirements
Installation
Node.js
npm install --save moment-holiday
var moment = require('moment-holiday');
moment().holiday('Christmas');
Browser
<script src="moment.js"></script>
<script src="moment-holiday.js"></script>
<script>
moment().isHoliday();
</script>
Bower
bower install --save moment-holiday
Functions
holiday
or holidays
Searches for holiday(s) by keywords. Returns a single moment object, an object containing moment objects with the holiday names as keys, or false
if no holidays were found.
Use
moment().holiday(holidays, adjust);
moment().holidays(holidays, adjust);
Parameters
- holidays - The holiday(s) to search for. Can be a string to search for a single holiday or an array to search for multiple. Defaults to all holidays.
- adjust - See global parameters.
Examples
moment().holiday('Memorial Day');
moment().holiday('Totally not a holiday');
moment().holiday(['Dad Day']);
moment().holidays(['Turkey Day', 'New Years Eve']);
moment().holidays(['Not actually a holiday', 'Mothers Day']);
moment('2018-01-01').holiday('Veterans Day');
moment('2018-01-01').holiday('Veterans Day', true);
moment().holidays();
isHoliday
Returns the name of the holiday (or true
if holidays
parameter is used) if the given date is in fact a holiday or false
if it isn't.
Use
moment().isHoliday(holidays, adjust);
Parameters
- holidays - Holidays to check for. Will cause function to return
true
if there is a match. Can be a string to compare with a single holiday or an array for multiple. Defaults to all holidays. - adjust - See global parameters.
Examples
moment('2017-12-25').isHoliday();
moment('2005-03-15').isHoliday();
moment('2009-10-31').isHoliday('Halloween');
moment('2017-12-31').isHoliday();
moment('2017-12-31').isHoliday(null, true);
previousHoliday
or previousHolidays
Returns an array (or a moment object if count
is set to 1
) containing the previous holidays before the given date.
Use
moment().previousHoliday(count, adjust);
moment().previousHolidays(count, adjust);
Parameters
- count - The number of previous holidays to fetch. Defaults to
1
. - adjust - See global parameters.
Examples
moment().previousHoliday();
moment('2001-02-14').previousHolidays(5);
moment('2001-02-14').previousHolidays(5, true);
nextHoliday
or nextHolidays
Returns an array (or a moment object if count
is set to 1
) containing the next holidays after the given date.
Use
moment().nextHoliday(count, adjust);
moment().nextHolidays(count, adjust);
Parameters
- count - The number of upcoming holidays to fetch. Defaults to
1
. - adjust - See global parameters.
Examples
moment().nextHoliday();
moment('2010-05-23').nextHolidays(5);
moment('2010-05-23').nextHolidays(5, true);
holidaysBetween
Returns an array containing the holidays between the given date and the date
parameter or false
if no dates were found.
Use
moment().holidaysBetween(date, adjust);
Parameters
- date - The end date range for holidays to get. Can be any string that moment accepts or a moment object. Defaults to today.
- adjust - See global parameters.
Examples
moment().holidaysBetween(moment().endOf('year'));
moment('2011-11-01').holidaysBetween('2011-12-31');
moment('2011-11-01').holidaysBetween('2011-12-31', true);
moment('2017-01-01').holidaysBetween();
Global Parameters
- adjust - Set to
true
to make all holidays that land on a Saturday go to the prior Friday and all holidays that land on a Sunday go to the following Monday. Defaults to false
.
The Holidays
The following holidays are built-in:
- New Year's Day
- Martin Luther King Jr. Day
- Valentine's Day
- Washington's Birthday
- Saint Patrick's Day
- Memorial Day
- Mother's Day
- Father's Day
- Independence Day
- Labor Day
- Columbus Day
- Halloween
- Veteran's Day
- Thanksgiving Day
- Day after Thanksgiving
- Christmas Eve
- Christmas Day
- New Year's Eve
Modifying Holidays
You can add and remove holidays by using the following helper functions:
modifyHolidays.set
moment().modifyHolidays.set(['New Years Day', 'Memorial Day', 'Thanksgiving']);
moment().holidays();
moment().modifyHolidays.set({
"My Birthday": {
date: '11/17',
keywords: ['my', 'birthday']
},
"Last Friday of the year": {
date: '12/(5,-1)',
keywords_y: ['friday']
}
});
moment().holidays();
modifyHolidays.add
moment().modifyHolidays.add({
"Inauguration Day": {
date: '1/20',
keywords_y: ['inauguration']
}
});
moment().holiday('Inauguration');
modifyHolidays.remove
moment().modifyHolidays.remove('Christmas');
moment().modifyHolidays.remove(['Dad Day', 'Mom Day', 'Saint Paddys Day']);
Holiday objects accept the following options:
-
date (Required) - The date of the holiday in the format of Month/Day
. A day wrapped in parentheses means a specific day of the week and expects two values separated by a comma. The first part is the day of the week as recognized by moment().day() (0=Sunday, 6=Saturday). The second part is the 1-indexed index of that day of week.
Examples:
5/20
- The 20th of May.7/(1,3)
- The third Monday of July.3/(4,-1)
- The last Thursday of March.
-
keywords - An array of optional keywords.
-
keywords_y - An array of required keywords.
-
keywords_n - An array of banned keywords.
View the source of moment-holiday.js for a better look at how the keywords work.
License
MIT. See the License file for more info.