Skip to content

Instantly share code, notes, and snippets.

@nicluo
Forked from jrhames/moment-holidays.js
Last active December 11, 2015 08:40
Show Gist options
  • Save nicluo/d0a270868ede6f3c5f3b to your computer and use it in GitHub Desktop.
Save nicluo/d0a270868ede6f3c5f3b to your computer and use it in GitHub Desktop.
//## Moment.JS Holiday Plugin for Singapore Public Holidays up until 2016
//
//Usage:
// Call .holiday() from any moment object. If date is a Singapore Public 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 'Y' and 'M' stands for Year and Month respectively,
// 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:
// Portions (c) 2014 [Nicholas Luo](http://nicluo.com)
// 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 = {
'Y': {
//Year, Month, Day
'14/01/31': "Chinese New Year",
'14/02/01': "Chinese New Year",
'14/04/18': "Good Friday",
'14/05/13': "Vesak Day",
'14/07/28': "Hari Raya Puasa",
'14/10/05': "Hari Raya Haji",
'14/10/06': "Hari Raya Haji Replacement",
'14/10/22': "Deepavali",
'15/02/19': "Chinese New Year",
'15/02/20': "Chinese New Year",
'15/04/03': "Good Friday",
'15/06/01': "Vesak Day",
'15/07/17': "Hari Raya Puasa",
'15/07/10': "National Day (Make Up)",
'15/09/24': "Hari Raya Haji",
'15/11/10': "Deepavali",
'16/02/08': "Chinese New Year",
'16/02/09': "Chinese New Year",
'16/03/25': "Good Friday",
'16/05/02': "Labour Day (Make Up)",
'16/05/21': "Vesak Day",
'16/07/06': "Hari Raya Puasa",
'16/09/12': "Hari Raya Haji",
'16/10/29': "Deepavali",
'16/12/26': "Christmas Day (Make Up)"
},
'M': {
//Month, Day
'01/01': "New Year's Day",
'05/01': "Labour Day",
'07/09': "National Day",
'12/25': "Christmas Day"
},
'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;
var year_based = _holidays['Y'][this.format('YY/MM/DD')];
var month_based = _holidays['M'][this.format('MM/DD')];
var week_day_based = _holidays['W'][this.format('M/'+ (memorial || diff) +'/d')];
return (year_based || month_based || week_day_based);
};
if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
module.exports = moment;
}
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment