Skip to content

Instantly share code, notes, and snippets.

@Thiruppathi
Created January 2, 2020 11:18
Show Gist options
  • Save Thiruppathi/508938c3a89aa7a8938000bb5da1dec1 to your computer and use it in GitHub Desktop.
Save Thiruppathi/508938c3a89aa7a8938000bb5da1dec1 to your computer and use it in GitHub Desktop.
Calculate Duration for courses
const durations = ["12:38", "6:36", "9:03", "8:34", "5:02", "6:54", "13:22", "4:41", "8:36", "21:58", "3:06", "10:46", "10:13", "12:54",
"14:00", "11:03", "16:03", "10:52", "24:53", "10:03", "11:49",
"15:47", "3:19", "2:06", "5:47", "1:03", "5:29", "5:47", "26:39"];


function sumDurations(durations) {
  return durations.reduce((sum, string) => {
    var mins, secs;
    [mins, secs] = string.split(":").slice(-2).map(n => parseInt(n, 10));
    return sum + mins * 60 + secs;
  }, 0);
}

function formatDuration(duration) {
  function pad(number) {
    return `${number}`.slice(-2);
  }

  let hours = duration / 3600 | 0;
  let minutes = duration % 3600 / 60 | 0;
  let seconds = duration % 60;
  let minsSecs = `${pad(minutes)}:${pad(seconds)}`;

  return hours > 0 ? `${hours}:${minsSecs}` : minsSecs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment