Skip to content

Instantly share code, notes, and snippets.

@jasonsperske
Last active October 11, 2024 17:56
Show Gist options
  • Save jasonsperske/af5d27e917e4b5f960bfc383f3e13eb1 to your computer and use it in GitHub Desktop.
Save jasonsperske/af5d27e917e4b5f960bfc383f3e13eb1 to your computer and use it in GitHub Desktop.
Dumb Number prototype overloading
Number.prototype.milliseconds = function() { return this }
Number.prototype.seconds = function() { return Number.prototype.milliseconds.call(this * 1000) }
Number.prototype.minutes = function() { return Number.prototype.seconds.call(this * 60) }
Number.prototype.hours = function() { return Number.prototype.minutes.call(this * 60) }
Number.prototype.days = function() { return Number.prototype.hours.call(this * 24) }
Number.prototype.weeks = function() { return Number.prototype.days.call(this * 7) }
Number.prototype.fortnights = function() { return Number.prototype.weeks.call(this * 2) }
Number.prototype.in = function() {
const that = this;
return {
milliseconds() { return that },
seconds() { return this.milliseconds() / 1000 },
minutes() { return this.seconds() / 60 },
hours() { return this.minutes() / 60 },
days() { return this.hours() / 24 },
weeks() { return this.days() / 7 },
fortnights() { return this.days() / 2 }
}
}
// for example
console.log((3).weeks().in().days())
//prints: 21
// or you can substract two dates and get the span between
const genesis = new Date(1989, 7, 14)
const nes = new Date(1986, 8, 27)
console.log((genesis-nes).in().weeks())
// prints: 150.28571428571428
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment