Skip to content

Instantly share code, notes, and snippets.

@imixtron
Last active January 29, 2018 14:39
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 imixtron/0eed4809e1cf40ac2a33c2fb8f6be7ed to your computer and use it in GitHub Desktop.
Save imixtron/0eed4809e1cf40ac2a33c2fb8f6be7ed to your computer and use it in GitHub Desktop.
/**
* Time Difference takes input in Hours Format
* Author: imixtron
* Accepts: String Values (HH:MM)
* Max: 24:00, Min: 00:00
*
* example timeDifference("10:37","12:35")
*/
timeDifference(from, to) {
let _from = from.split(':');
let _to = to.split(':');
//TODO: ERROR HANDLING FOR MAX / MIN
let _fromHours = _from[0];
let _toHours = _to[0];
let _fromMinutes = _from[1];
let _toMinutes = _to[1];
let diffHours = _toHours - _fromHours;
let diffMinutes = _toMinutes - _fromMinutes;
let extraHours = diffMinutes < 0 ? 1 : 0;
diffMinutes = diffMinutes < 0 ? diffMinutes + 60 : diffMinutes;
return (diffHours - extraHours) + ':' + (diffMinutes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment