Created
December 9, 2016 14:33
-
-
Save jpwilliams/d9d15b5d6708b1d19d098d2d39f3375b to your computer and use it in GitHub Desktop.
Add methods to the Date class to allow incrementing and decrementing dates
This file contains 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
var types = [ | |
'Date', | |
'FullYear', | |
'Hours', | |
'Milliseconds', | |
'Minutes', | |
'Month', | |
'Seconds', | |
'UTCDate', | |
'UTCFullYear', | |
'UTCHours', | |
'UTCMilliseconds', | |
'UTCMinutes', | |
'UTCMonth', | |
'UTCSeconds' | |
] | |
types.forEach(function (type) { | |
Date.prototype['increment' + type] = function (amount) { | |
this['set' + type](this['get' + type]() + amount) | |
return this | |
} | |
Date.prototype['decrement' + type] = function (amount) { | |
this['set' + type](this['get' + type]() - amount) | |
return this | |
} | |
}) | |
var foo = new Date() | |
foo.incrementMinutes(42) | |
foo.decrementHours(20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment