Skip to content

Instantly share code, notes, and snippets.

@basith374
Created May 21, 2019 12:05
Show Gist options
  • Save basith374/7876e116ce533e22f5df2f90d533d145 to your computer and use it in GitHub Desktop.
Save basith374/7876e116ce533e22f5df2f90d533d145 to your computer and use it in GitHub Desktop.
moment better duration
export function duration(ms) {
if(!ms) return 'N/A';
if(ms < (60 * 1000)) {
let dur = Math.floor(ms / 1000);
if(dur < 2) return 'a second';
return dur + ' seconds';
}
if(ms < (60 * 60 * 1000)) {
let minutes = Math.floor(ms / 1000 / 60);
let seconds = Math.floor(ms % (1000 * 60) / 1000);
let dur = minutes + ' min ';
if(seconds) dur += seconds + ' sec';
return dur;
}
return moment.duration(ms).humanize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment