Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement