Created
April 7, 2018 13:52
-
-
Save sjorsvanheuveln/1a517f6b23e226d3d0b7dd556d7b3e62 to your computer and use it in GitHub Desktop.
A simple Time object that allows you to add, subtract and print times.
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
export function Time(hours, minutes) { | |
this.h = hours; | |
this.m = minutes; | |
this.add = (h, m) => { | |
this.h = (this.h + h) % 24; | |
this.h = this.h + Math.floor((this.m + m) / 60); | |
this.m = (this.m + m) % 60; | |
return this; // for method chaining | |
}; | |
this.subtract = (h, m) => { | |
this.h = (24 + (this.h - h)) % 24; | |
this.h = this.h - Math.floor((59 + (m - this.m)) / 60); | |
this.m = (60 + (this.m - m)) % 60; | |
return this; | |
}; | |
this.toString = () => `0${(this.h)}`.slice(-2).concat(':').concat(`0${(this.m)}`.slice(-2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment