Skip to content

Instantly share code, notes, and snippets.

@rd13
Last active October 15, 2021 13:40
Show Gist options
  • Save rd13/fe0c8ba1a566bdbb272d7f8d322d053f to your computer and use it in GitHub Desktop.
Save rd13/fe0c8ba1a566bdbb272d7f8d322d053f to your computer and use it in GitHub Desktop.
Encode an iso8601 duration in Javascript
/**
* Utility function to convert seconds to an iso8601 duration string.
*
* @param seconds int The number of seconds to encode.
* @return string iso8601 formatted duration
*/
const iso8601Duration = (seconds = 0) => {
let _seconds = seconds;
const days = Math.floor(_seconds / 86400);
_seconds %= 86400;
const hours = Math.floor(_seconds / 3600);
_seconds %= 3600;
const minutes = Math.floor(_seconds / 60);
_seconds %= 60;
return `P${days}DT${hours}H${minutes}M${_seconds}S`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment