Skip to content

Instantly share code, notes, and snippets.

@aznoisib
Forked from flangofas/ConvertMS.js
Created February 19, 2021 17:06
Show Gist options
  • Save aznoisib/5f8cd3926a130b8146cd8f83e89fe638 to your computer and use it in GitHub Desktop.
Save aznoisib/5f8cd3926a130b8146cd8f83e89fe638 to your computer and use it in GitHub Desktop.
JS: Convert Milliseconds to days? minutes? seconds? or all!
function convertMiliseconds(miliseconds, format) {
var days, hours, minutes, seconds, total_hours, total_minutes, total_seconds;
total_seconds = parseInt(Math.floor(miliseconds / 1000));
total_minutes = parseInt(Math.floor(total_seconds / 60));
total_hours = parseInt(Math.floor(total_minutes / 60));
days = parseInt(Math.floor(total_hours / 24));
seconds = parseInt(total_seconds % 60);
minutes = parseInt(total_minutes % 60);
hours = parseInt(total_hours % 24);
switch(format) {
case 's':
return total_seconds;
case 'm':
return total_minutes;
case 'h':
return total_hours;
case 'd':
return days;
default:
return { d: days, h: hours, m: minutes, s: seconds };
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment