Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active May 6, 2017 10:12
Show Gist options
  • Save myfreeer/2dcda6e49fff0e0ab9e114c8cfa92045 to your computer and use it in GitHub Desktop.
Save myfreeer/2dcda6e49fff0e0ab9e114c8cfa92045 to your computer and use it in GitHub Desktop.
const str2sec = str => str.split(':').reduce((acc, val)=>Number(acc)*60 + Number(val));
const sec2str = t => t/60|0 > 0 ? sec2str(t/60|0) + ':' + t%60 : t + '';
//demo below
var timeStr = '3:44:45.466'; // 3 hour, 44 min, 45 sec, 466 ms
var timeSec = str2sec(timeStr); // 13485.466 sec
sec2str(timeSec); // "3:44:45.46600000000035"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment