Skip to content

Instantly share code, notes, and snippets.

@shanejdonnelly
Created February 5, 2015 21:46
Show Gist options
  • Save shanejdonnelly/ddd2fd3f2c581d16bc87 to your computer and use it in GitHub Desktop.
Save shanejdonnelly/ddd2fd3f2c581d16bc87 to your computer and use it in GitHub Desktop.
Extend javascript Date object with handy day and month name methods.
Date.locale = {
en: {
day_names: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
day_names_short: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
month_names: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
month_names_short: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}
};
//====================================================
//
//Day Names
//
//====================================================
Date.prototype.getDayName = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].day_names[this.getDay()];
};
Date.prototype.getDayNameShort = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].day_names_short[this.getDay()];
};
//====================================================
//
//Month Names
//
//====================================================
Date.prototype.getMonthName = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names[this.getMonth()];
};
Date.prototype.getMonthNameShort = function(lang) {
lang = lang && (lang in Date.locale) ? lang : 'en';
return Date.locale[lang].month_names_short[this.getMonth()];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment