Last active
October 15, 2021 13:40
-
-
Save rd13/fe0c8ba1a566bdbb272d7f8d322d053f to your computer and use it in GitHub Desktop.
Encode an iso8601 duration in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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