Created
October 22, 2018 18:22
-
-
Save productioncoder/35283cd0998446c43d4c365ebca99b19 to your computer and use it in GitHub Desktop.
Youtube format video duration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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