A Moment.js plugin for handling holidays.
Since Moment.js is being deprecated, it only makes sense to deprecate moment-holiday as well. Feel free to fork and use this code in any way you wish however I will no longer be maintaining it.
- moment.js v2.0.0 or higher
npm install --save moment-holiday
var moment = require('moment-holiday');
moment().holiday('Christmas');
<script src="moment.js"></script>
<script src="moment-holiday.js"></script>
<script>
moment().isHoliday();
<
8000
;/script>
bower install --save moment-holiday
moment-holiday.js
does not come with any locales built-in by default. However, the following files are included for your convenience:
build/moment-holiday-pkg.min.js
- moment-holiday with all available locales built-in.build/moment-holiday-us.min.js
- moment-holiday with theUnited States
locale built-in.build/moment-holiday.min.js
- Minified version of moment-holiday with no locales built-in.
You can generate the above files by running gulp build
.
You can also generate your own custom builds of moment-holiday by using gulp with the following options:
- name - The name of the file to generate. (Defaults to
moment-holiday-custom.js
) - locale - The locale(s) you would like included in the build. Pass this option multiple times to include multiple locales.
- set - The locale(s) to have set by default in the build. Pass this option multiple times to have multiple locales set by default.
- min - Pass this option if you would like the generated file to be minified.
For example:
gulp --name=moment-holiday-ar.js --locale=Argentina --locale=Easter --set=Argentina --min
Sourcemaps are automatically created for all minified builds.
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.
moment().holiday(holidays, adjust);
//or
moment().holidays(holidays, adjust);
- 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.
moment().holiday('Memorial Day');
//moment("2017-05-29T00:00:00.000")
moment().holiday('Totally not a holiday');
//false
moment().holiday(['Dad Day']);
//{ 'Father\'s Day': moment("2017-06-18T00:00:00.000") }
moment().holidays(['Turkey Day', 'New Years Eve']);
//{ 'Thanksgiving Day': moment("2017-11-23T00:00:00.000"),
// 'New Year\'s Eve': moment("2017-12-31T00:00:00.000") }
moment().holidays(['Not actually a holiday', 'Mothers Day']);
//{ 'Mother\'s Day': moment("2017-05-14T00:00:00.000") }
moment('2018-01-01').holiday('Veterans Day');
//moment("2018-11-11T00:00:00.000")
moment('2018-01-01').holiday('Veterans Day', true);
//moment("2018-11-12T00:00:00.000")
moment().holidays();
//Returns all holidays
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. Will return an array of holiday names if multiple holidays land on that same day.
moment().isHoliday(holidays, adjust);
- 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.
moment('2017-12-25').isHoliday();
//Christmas Day
moment('2005-03-15').isHoliday();
//false
moment('2009-10-31').isHoliday('Halloween');
//true
moment('2017-12-31').isHoliday();
//New Year's Eve
moment('2017-12-31').isHoliday(null, true);
//false
moment('2017-04-17').isHoliday(null, true);
//[ 'Easter Sunday', 'Easter Monday' ]
or previousHolidays
Returns an array (or a moment object if count
is set to 1
) containing the previous holidays before the given date.
moment().previousHoliday(count, adjust);
//or
moment().previousHolidays(count, adjust);
- count - The number of previous holidays to fetch. Defaults to
1
. - adjust - See global parameters.
moment().previousHoliday();
//moment("2017-07-04T00:00:00.000")
moment('2001-02-14').previousHolidays(5);
//[ moment("2001-01-15T00:00:00.000"),
// moment("2001-01-01T00:00:00.000"),
// moment("2000-12-31T00:00:00.000"),
// moment("2000-12-25T00:00:00.000"),
// moment("2000-12-24T00:00:00.000") ]
moment('2001-02-14').previousHolidays(5, true);
//[ moment("2001-01-15T00:00:00.000"),
// moment("2001-01-01T00:00:00.000"),
// moment("2000-12-25T00:00:00.000"),
// moment("2000-11-24T00:00:00.000"),
// moment("2000-11-23T00:00:00.000") ]
moment().previousHoliday().isHoliday();
//Independence Day
or nextHolidays
Returns an array (or a moment object if count
is set to 1
) containing the next holidays after the given date.
moment().nextHoliday(count, adjust);
//or
moment().nextHolidays(count, adjust);