Skip to content

Instantly share code, notes, and snippets.

@salipro4ever
Created June 15, 2016 09:27
Show Gist options
  • Save salipro4ever/1daf731c84bce064a0d01019fed48dd6 to your computer and use it in GitHub Desktop.
Save salipro4ever/1daf731c84bce064a0d01019fed48dd6 to your computer and use it in GitHub Desktop.
add time JS
Date.prototype.addSeconds = function(seconds) {
this.setSeconds(this.getSeconds() + seconds);
return this;
};
Date.prototype.addMinutes = function(minutes) {
this.setMinutes(this.getMinutes() + minutes);
return this;
};
Date.prototype.addHours = function(hours) {
this.setHours(this.getHours() + hours);
return this;
};
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + days);
return this;
};
Date.prototype.addWeeks = function(weeks) {
this.addDays(weeks*7);
return this;
};
Date.prototype.addMonths = function (months) {
var dt = this.getDate();
this.setMonth(this.getMonth() + months);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
Date.prototype.addYears = function(years) {
var dt = this.getDate();
this.setFullYear(this.getFullYear() + years);
var currDt = this.getDate();
if (dt !== currDt) {
this.addDays(-currDt);
}
return this;
};
===================
var now = new Date();
console.log(now.addWeeks(3));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment