Skip to content

Instantly share code, notes, and snippets.

Created September 5, 2016 20:25
Show Gist options
  • Save anonymous/8124915bfe71da15f63ef9fdaac0d2b5 to your computer and use it in GitHub Desktop.
Save anonymous/8124915bfe71da15f63ef9fdaac0d2b5 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