Skip to content

Instantly share code, notes, and snippets.

@vnegrisolo
Created June 20, 2016 19:56
Show Gist options
  • Save vnegrisolo/e61c5e8592c8cbc258240a6564a9d572 to your computer and use it in GitHub Desktop.
Save vnegrisolo/e61c5e8592c8cbc258240a6564a9d572 to your computer and use it in GitHub Desktop.
Some Date monkey patches, because I don't need to use `moment js` and javascript Date is as useful as airplane horn
Date.prototype.advanceDays = function(days) {
let d = new Date(this);
return new Date(d.setDate(this.getDate() + days));
};
Date.prototype.beginningOfWeek = function() {
return this.advanceDays((this.getDay() == 0 ? -6 : 1) - this.getDay());
};
Date.prototype.getWeekName = function() {
return ['S', 'M', 'T', 'W', 'T', 'F', 'S'][this.getDay()];
};
Date.prototype.getMonthName = function() {
return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][this.getMonth()];
};
Date.prototype.queryFormat = function() {
return this.getFullYear() + '-' + (this.getMonth() + 1) + '-' + this.getDate();
};
Date.prototype.simpleFormat = function() {
return this.getDate() + ' ' + this.getMonthName();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment