Created
March 1, 2021 13:08
-
-
Save dmitry-stepanenko/8a08f533e3fe7113bfad370a0139e3ff to your computer and use it in GitHub Desktop.
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
/** | |
* Returns duration in ms from ISO 8601 string. | |
* E.g. "PT15M33S" is converted to 933000 | |
*/ | |
export function convertISO8601ToMs(duration: string): number { | |
const time_extractor = /^P([0-9]*D)?T([0-9]*H)?([0-9]*M)?([0-9]*S)?$/i; | |
const extracted = time_extractor.exec(duration); | |
if (extracted) { | |
const days = parseInt(extracted[1], 10) || 0; | |
const hours = parseInt(extracted[2], 10) || 0; | |
const minutes = parseInt(extracted[3], 10) || 0; | |
const seconds = parseInt(extracted[4], 10) || 0; | |
return (days * 24 * 3600 * 1000) + (hours * 3600 * 1000) + (minutes * 60 * 1000) + (seconds * 1000); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment