ISO

 avatar
unknown
javascript
2 years ago
665 B
13
Indexable
function iso8601ToSeconds(isoDateString) {
    // Parse the ISO 8601 string using a regular expression
    const matches = /PT((\d+)H)?((\d+)M)?((\d+)S)?/.exec(isoDateString);
    if (!matches) {
        throw new Error("Invalid ISO 8601 duration format");
    }
    // Extract the hours, minutes, and seconds components from the matches
    const hours = parseInt(matches[2] || "0", 10);
    const minutes = parseInt(matches[4] || "0", 10);
    const seconds = parseInt(matches[6] || "0", 10);
    // Convert the duration to seconds
    const totalSeconds = hours * 3600 + minutes * 60 + seconds;
    return totalSeconds;
}

console.log(iso8601ToSeconds("PT60M"));
Editor is loading...