Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
};
}
@matheusmurta
Copy link

matheusmurta commented Jan 16, 2018

thanks !

@FahadAlbukhari
Copy link

FahadAlbukhari commented Jun 21, 2018

Thank you very much

@NicolasDutour
Copy link

NicolasDutour commented Jun 22, 2018

Thank you but how do you call it ?

for exemple display the minutes:
console.log( ???? );

@fawadmukhtar
Copy link

fawadmukhtar commented Sep 14, 2018

Thanks! , Great Work

@121watts
Copy link

121watts commented Oct 18, 2018

thank you!

@121watts
Copy link

121watts commented Oct 18, 2018

@NicolasDutour

Thank you but how do you call it ?

for exemple display the minutes:
console.log( ???? );

var durations =  convertMS(<ms goes here>)
console.log(durations.minute)

@firewarher1234
Copy link

firewarher1234 commented Jan 12, 2019

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

@Adam-Salt
Copy link

Adam-Salt commented Jan 23, 2019

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

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

@achata
Copy link

achata commented Feb 28, 2019

Thanks !!

@Pfuufy
Copy link

Pfuufy commented Jan 8, 2020

You're the best! Thank you so much!

@nz-nz
Copy link

nz-nz commented Mar 15, 2020

Thanks!

@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

cbljamon commented May 27, 2020

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

@eightalex
Copy link

eightalex commented Nov 9, 2020

thank you

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