Skip to content

Instantly share code, notes, and snippets.

@sjorsvanheuveln
Created April 7, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjorsvanheuveln/1a517f6b23e226d3d0b7dd556d7b3e62 to your computer and use it in GitHub Desktop.
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.
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