Skip to content

Instantly share code, notes, and snippets.

@QueueHammer
Last active December 27, 2020 21:55
Show Gist options
  • Select an option

  • Save QueueHammer/cb9bf736b81029d65485fb30ad01918d to your computer and use it in GitHub Desktop.

Select an option

Save QueueHammer/cb9bf736b81029d65485fb30ad01918d to your computer and use it in GitHub Desktop.
Creation of a isMaxDate and isMinDate for the Date object in JavaScript.
//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);
@avinoamsn

Copy link
Copy Markdown

Thanks for this little tool. I recommend the function be separated from the Date object to avoid prototype pollution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment