Skip to content

Instantly share code, notes, and snippets.

@MD4
Created October 2, 2018 09:12
Show Gist options
  • Save MD4/568cdbcd37e32aaa7d7bf493721af630 to your computer and use it in GitHub Desktop.
Save MD4/568cdbcd37e32aaa7d7bf493721af630 to your computer and use it in GitHub Desktop.
const formatToDuration = milliseconds => {
const seconds = Math.round(milliseconds / 1000);
const s = Math.round(seconds % 60);
const m = (Math.floor(seconds / 60) % 60);
const h = (Math.floor(seconds / 60 / 60) % 24);
const d = (Math.floor(seconds / 60 / 60 / 24));
return [
(d ? `${d}d` : ''),
(h ? `${h}h` : ''),
(m ? `${m}m` : ''),
(`${s}s`),
].filter(a => a).join(' ');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment