Last active
December 27, 2020 21:55
-
-
Save QueueHammer/cb9bf736b81029d65485fb30ad01918d to your computer and use it in GitHub Desktop.
Creation of a isMaxDate and isMinDate for the Date object in JavaScript.
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
//Inspired by http://stackoverflow.com/a/11526569/46810 | |
(function () { | |
//Max possible value for Date() +/- of 0 | |
var max = 8640000000000000; | |
Object.defineProperties(Date.prototype, { | |
isMaxDate: { | |
get: function () { return this.valueOf() === max; } | |
}, | |
isMinDate: { | |
get: function () { return this.valueOf() === -max; } | |
} | |
}); | |
})(); | |
console.log((new Date(8640000000000000)).isMaxDate); | |
console.log((new Date(8640000000000000)).isMinDate); | |
console.log((new Date(-8640000000000000)).isMaxDate); | |
console.log((new Date(-8640000000000000)).isMinDate); | |
console.log((new Date(5)).isMaxDate); | |
console.log((new Date(-5)).isMinDate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this little tool. I recommend the function be separated from the Date object to avoid prototype pollution.