Skip to content

Instantly share code, notes, and snippets.

@flangofas
Last active March 28, 2025 14:13
Show Gist options
  • Select an option

  • Save flangofas/714f401b63a1c3d84aaa to your computer and use it in GitHub Desktop.

Select an option

Save flangofas/714f401b63a1c3d84aaa 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 };
}
};
@Dias999

Dias999 commented Jan 10, 2019

Copy link
Copy Markdown

This is awesome! Thank you!

@Captainfive

Copy link
Copy Markdown

Thx dude, just one thing, breaking after a return isn't useful. the break is unreachable.

@flangofas

Copy link
Copy Markdown
Author

Thanks! Missed that ;)

@wkok

wkok commented Jun 2, 2020

Copy link
Copy Markdown

Thank you, wish I found your solution earlier, would've saved me 2 days.

@aaronangle

Copy link
Copy Markdown

Thanks this works great!

@joaovct

joaovct commented Sep 18, 2020

Copy link
Copy Markdown

Thanks 👏🏽!

@Nethanos

Copy link
Copy Markdown

Thanks! This is awesome!

@petrone7

petrone7 commented Dec 1, 2020

Copy link
Copy Markdown

Thank you very much! works like a charm!

@godfern

godfern commented Dec 9, 2020

Copy link
Copy Markdown

Looks like the days giving a wrong value. The calculation for a day is giving an extra day.

Eg: if the difference between 2 dates i.e. from date: 02/12/2020 and to date: 09/12/2020 gives 8 days instead 7 days

@aznoisib

Copy link
Copy Markdown

thx bro

@haydanu

haydanu commented Apr 19, 2021

Copy link
Copy Markdown

nice

@maxmatyugin

Copy link
Copy Markdown

thx! Very helpful!

@ARIPRASATH4664

Copy link
Copy Markdown

Love

@DET171

DET171 commented Oct 20, 2021

Copy link
Copy Markdown

+1

@neilbannet

Copy link
Copy Markdown

You are a STAR!

@waldothedeveloper

Copy link
Copy Markdown

Beautiful! Thanks

@medamin25

medamin25 commented Aug 30, 2022

Copy link
Copy Markdown

simple real time calculator for this https://jsfiddle.net/m90hven1/

@nforne

nforne commented Feb 8, 2023

Copy link
Copy Markdown

Good job!
Principle : "Fail fast and early if you have to and recover soonest"
So I'll suggest you return zero on the third line if the milliseconds argument is zero, or any out of order input.
Some sort of error handling I suppose.
It saves on infrastructure cost and improves app performance when the other lines of code don't have to pointlessly run .
Like so .......
msTimeConverter

@danilovsetin

Copy link
Copy Markdown

Thanks a lot!

@KelvinNjihia

Copy link
Copy Markdown

Thanks a bunch!

@Mau5trakt

Copy link
Copy Markdown

that's everything i needed

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