Skip to content

Instantly share code, notes, and snippets.

@yussan
Created October 6, 2021 13:23
Show Gist options
  • Save yussan/b9c7382cba66f96df8c6a2879764cbee to your computer and use it in GitHub Desktop.
Save yussan/b9c7382cba66f96df8c6a2879764cbee to your computer and use it in GitHub Desktop.
Convert Miliseconds to hh:ii:ss:ms
/**
* @desc function to convert ms to time format hh:ii:ss.ms
* @param {Number} duration, time ini miliseconds
* @return {String} sample 00:01:01:99
*/
export function msToTime(duration = 0) {
let milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
milliseconds = milliseconds < 10 ? "0" + milliseconds : milliseconds;
return `${hours}:${minutes}:${seconds}:${milliseconds}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment