Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Youtube format video duration
export function getVideoDurationString(iso8601DateString) {
if (!iso8601DateString || iso8601DateString === '') {
return '';
}
// new Date(Date.parse(...)) doesn't work here
// therefore we are using our regex approach
let {days, hours, minutes, seconds} = parseISO8601TimePattern(iso8601DateString);
let secondsString = seconds.toString();
let minutesString = minutes.toString();
let accumulatedHours = days * 24 + hours;
if (seconds < 10) {
secondsString = seconds.toString().padStart(2, '0');
}
if (minutes < 10 && hours !== 0) {
minutesString = minutesString.toString().padStart(2, '0');
}
if (!accumulatedHours) {
return [minutesString, secondsString].join(':');
} else {
return [accumulatedHours, minutesString, secondsString].join(':');
}
}
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.