Skip to content

Instantly share code, notes, and snippets.

/example.js Secret

Created August 25, 2016 06:07
Show Gist options
  • Save anonymous/56c9858c7e81d6c4da26dc8578d0c091 to your computer and use it in GitHub Desktop.
Save anonymous/56c9858c7e81d6c4da26dc8578d0c091 to your computer and use it in GitHub Desktop.
function timeFormatter (milliseconds) {
const padZero = (time) => `0${time}`.slice(-2);
const minutes = padZero(milliseconds / 60000 | 0);
const seconds = padZero((milliseconds / 1000 | 0) % 60);
const centiseconds = padZero((Math.round(milliseconds / 10) | 0) % 100);
return `${minutes} : ${seconds} . ${centiseconds}`;
}
// Example stopwatch times
const timeIntervals = [
{
startTime: 1472104779284,
stopTime: 1472104782441
},
{
startTime: 1472104782442,
stopTime: 1472104785081
},
{
startTime: 1472104785081,
stopTime: 1472104788368
},
{
startTime: 1472104788369,
stopTime: 1472104791475
}
];
// Calculate time it took for each entry
const times = timeIntervals.map(time => time.stopTime - time.startTime);
// Run the timeFormatter on each individual time
const individualTimes = times.map(timeFormatter);
// Run the timeFormatter on the sum of all the times
const mainTimer = timeFormatter(times.reduce((a, b) => a + b));
/**
* [
* '00 : 03 . 16',
* '00 : 02 . 64',
* '00 : 03 . 29',
* '00 : 03 . 11'
* ]
*
* (Adds up to 00 : 12 . 20)
*/
console.log(individualTimes);
/**
* 00 : 12 . 19
*/
console.log(mainTimer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment