Format Duration

Format a duration in seconds to a human readable format like '1h 2m 3s'.

Code

Utilities
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = seconds % 60;
return [h && `${h}h`, m && `${m}m`, `${s}s`].filter(Boolean).join(' ');

Parameters

Duration in seconds.

Browser·fetch() may be limited by CORS

More JavaScript Snippets