Skip to content

Instantly share code, notes, and snippets.

@jpwilliams
Created December 9, 2016 14:33
Show Gist options
  • Save jpwilliams/d9d15b5d6708b1d19d098d2d39f3375b to your computer and use it in GitHub Desktop.
Save jpwilliams/d9d15b5d6708b1d19d098d2d39f3375b to your computer and use it in GitHub Desktop.
Add methods to the Date class to allow incrementing and decrementing dates
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