Skip to content

Instantly share code, notes, and snippets.

@Erichain
Forked from remino/msconvert.js
Last active December 25, 2023 19:15
Show Gist options
  • Save Erichain/6d2c2bf16fe01edfcffa to your computer and use it in GitHub Desktop.
Save Erichain/6d2c2bf16fe01edfcffa to your computer and use it in GitHub Desktop.
JavaScript: Convert milliseconds to object with days, hours, minutes, and seconds
function convertMS( milliseconds ) {
var day, hour, minute, seconds;
seconds = Math.floor(milliseconds / 1000);
minute = Math.floor(seconds / 60);
seconds = seconds % 60;
hour = Math.floor(minute / 60);
minute = minute % 60;
day = Math.floor(hour / 24);
hour = hour % 24;
return {
day: day,
hour: hour,
minute: minute,
seconds: seconds
};
}
@dagyrox
Copy link

dagyrox commented Mar 23, 2020

How could I use this function for years, months, weeks, days, hours and minutes?

var years = Math.floor(days / 365);

trying to accomodate leap years will be a pain 🤢

@dagyrox
Copy link

dagyrox commented Mar 23, 2020

awesome work, thank you

@cbljamon
Copy link

great bro....you just save my time...thanku so much

@eightalex
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment