Skip to content

Instantly share code, notes, and snippets.

@alex-dinh
Created April 10, 2019 20:27
Show Gist options
  • Select an option

  • Save alex-dinh/1e9df5382b17b52075919920e9c44473 to your computer and use it in GitHub Desktop.

Select an option

Save alex-dinh/1e9df5382b17b52075919920e9c44473 to your computer and use it in GitHub Desktop.
exports.at = at;
function at(hours, minutes) {
let hr = hours;
let min = 0;
if (minutes) {
hr += Math.floor(minutes / 60);
min = minutes;
}
// overflow calculations for hours and minutes
hr = (hr >= 0 ? hr % 24 : 24 + hr % 24);
min = (min >= 0 ? min % 60 : 60 + min % 60);
return formatTime(hr) + ':' + formatTime(min);
}
function formatTime(time) { // used to format both hours and minutes
if (time < 10) { // if hours/minutes is a single digit number, prepend 0
return '0' + time.toString();
} else {
return time.toString();
}
}
String.prototype.plus = function(minutes) {
let time = this.split(':');
let hr = parseInt(time[0]);
let min = parseInt(time[1]);
min += minutes;
hr += Math.floor(min / 60);
min %= 60;
hr %= 24;
return formatTime(hr) + ':' + formatTime(min);
};
String.prototype.minus = function(minutes) {
let time = this.split(':');
let hr = parseInt(time[0]);
let min = parseInt(time[1]);
min -= minutes;
hr += Math.floor(min / 60);
min = (min >= 0 ? min % 60 : 60 + min % 60);
hr = (hr >= 0 ? hr % 24 : 24 + hr % 24);
if (hr % 24 === 0) hr %= 24; // handles negative integer multiples 24
return formatTime(hr) + ':' + formatTime(min);
};
String.prototype.equals = function(time2) {
return this === time2;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment