Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. exports.at = at;
  2.  
  3. function at(hours, minutes) {
  4. let hr = hours;
  5. let min = 0;
  6.  
  7. if (minutes) {
  8. hr += Math.floor(minutes / 60);
  9. min = minutes;
  10. }
  11. // overflow calculations for hours and minutes
  12. hr = (hr >= 0 ? hr % 24 : 24 + hr % 24);
  13. min = (min >= 0 ? min % 60 : 60 + min % 60);
  14.  
  15. return formatTime(hr) + ':' + formatTime(min);
  16. }
  17.  
  18. function formatTime(time) { // used to format both hours and minutes
  19. if (time < 10) { // if hours/minutes is a single digit number, prepend 0
  20. return '0' + time.toString();
  21. } else {
  22. return time.toString();
  23. }
  24. }
  25.  
  26. String.prototype.plus = function(minutes) {
  27. let time = this.split(':');
  28. let hr = parseInt(time[0]);
  29. let min = parseInt(time[1]);
  30.  
  31. min += minutes;
  32. hr += Math.floor(min / 60);
  33. min %= 60;
  34. hr %= 24;
  35.  
  36. return formatTime(hr) + ':' + formatTime(min);
  37. };
  38.  
  39. String.prototype.minus = function(minutes) {
  40. let time = this.split(':');
  41. let hr = parseInt(time[0]);
  42. let min = parseInt(time[1]);
  43.  
  44. min -= minutes;
  45. hr += Math.floor(min / 60);
  46.  
  47. min = (min >= 0 ? min % 60 : 60 + min % 60);
  48. hr = (hr >= 0 ? hr % 24 : 24 + hr % 24);
  49. if (hr % 24 === 0) hr %= 24; // handles negative integer multiples 24
  50.  
  51. return formatTime(hr) + ':' + formatTime(min);
  52. };
  53.  
  54. String.prototype.equals = function(time2) {
  55. return this === time2;
  56. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement