Skip to content

Instantly share code, notes, and snippets.

@jonathascosta
Forked from anonymous/DateExtensions.js
Created September 5, 2016 20:26
Show Gist options
  • Save jonathascosta/e6293748de47e59ef741274fd27e1fa8 to your computer and use it in GitHub Desktop.
Save jonathascosta/e6293748de47e59ef741274fd27e1fa8 to your computer and use it in GitHub Desktop.
Date Extensions for Javascript
Date.prototype.clone = function () {
return new Date(this.valueOf());
}
Date.prototype.addDays = function (days) {
var d = this.clone();
d.setDate(d.getDate() + days);
return d;
}
Date.prototype.countDays = function (end) {
var start = this.getTime();
return Math.round((end.getTime() - this.getTime()) / (24 * 60 * 60 * 1000))
}
Date.prototype.countMonths = function (end) {
return (end.getFullYear() * 12 + end.getMonth()) - (this.getFullYear() * 12 + this.getMonth());
}
Date.prototype.datePart = function () {
var d = this.clone();
d.setHours(0, 0, 0, 0);
return d;
}
Date.prototype.weekIndexInMonth = function () {
return Math.ceil(this.getDate() / 7) - 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment