Skip to content

Instantly share code, notes, and snippets.

@ariamoraine
Created June 27, 2017 16:30
Show Gist options
  • Save ariamoraine/ad2284411e677a6f88105887359fac6f to your computer and use it in GitHub Desktop.
Save ariamoraine/ad2284411e677a6f88105887359fac6f to your computer and use it in GitHub Desktop.

Prompt

Given a current time in the string format HH:MM and a number of minutes, return the clock time given those minutes added to the base time. Assume you're working with a standard 12-hour clock and the output should match the input format HH:MM.

Examples

addMinutes('1:30', 30);     // 2:00
addMinutes('12:30', 40);    // 1:10
addMinutes('11:59', 1);     // 12:00
addMinutes('1:59', 240);    // 5:59
addMinutes('1:23', 456789); // 6:32

Solutions

The solution below uses modular math. We can calculate the "total" minutes by adding the current minute hand value to the given minutes to add. From there, if we mod by 60 we'll get the remainder, which tells us where the minute hand would end up. For the hours, we can follow a similar process: add the current hour hand value to the given "hours to add" (which we can get by dividing the total minutes by 60) and then mod by 12.

There's one edge case to consider. The hours are "one-indexed" not "zero-indexed", i.e. there's no such time as 0:40 (well for non-military clocks), instead it's 12:40. To solve this, we can subtract 1 then mod by 12 then add 1.

function addMinutes (oldTime, minLater) {
  const [oldHrs, oldMins] = oldTime.split(':').map(str => Number(str));
  
  const oldTotalMins = (oldHrs * 60) + oldMins;
  const newTotalMins = oldTotalMins + minLater;
  const totalHrs = Math.floor(newTotalMins / 60);
  const newHrs = ((totalHrs - 1) % 12) + 1;
  const newMins = newTotalMins % 60;
  
  return `${newHrs}:${newMins > 9 ? newMins : `0${newMins}`}`;
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment