Created
June 15, 2016 09:27
-
-
Save salipro4ever/1daf731c84bce064a0d01019fed48dd6 to your computer and use it in GitHub Desktop.
add time JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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