Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parse ISO8601 time string
const objMap = ['years', 'months','days', 'hours', 'minutes', 'seconds'];
const numbers = '\\d+(?:[\\.,]\\d{0,3})?';
const datePattern = `(${numbers}Y)?(${numbers}M)?(${numbers}D)?`;
const timePattern = `T(${numbers}H)?(${numbers}M)?(${numbers}S)?`;
const pattern = new RegExp(`P(?:${datePattern}(?:${timePattern})?)`);
export function parseISO8601TimePattern(durationString) {
// https://github.com/tolu/ISO8601-duration/blob/master/src/index.js
return durationString.match(pattern).slice(1).reduce((prev, next, idx) => {
prev[objMap[idx]] = parseFloat(next) || 0;
return prev
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.