Skip to content

Instantly share code, notes, and snippets.

@productioncoder
Created October 22, 2018 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save productioncoder/35283cd0998446c43d4c365ebca99b19 to your computer and use it in GitHub Desktop.
Save productioncoder/35283cd0998446c43d4c365ebca99b19 to your computer and use it in GitHub Desktop.
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