ISO

mail@pastecode.io avatar
unknown
javascript
a year ago
665 B
10
Indexable
Never
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"));