Skip to content

Instantly share code, notes, and snippets.

@simplesessions
Created June 2, 2020 23:54
Show Gist options
  • Save simplesessions/34784aebc8bc55dd95ed265670cb4258 to your computer and use it in GitHub Desktop.
Save simplesessions/34784aebc8bc55dd95ed265670cb4258 to your computer and use it in GitHub Desktop.
convert-yt-timestamp.js
const str = '2m20s'
const seconds = str.split(/(\d+\D)/ig).filter(n => n).map(s => s.split(/(\d+)/).filter(n => n)).reduce((acc, curr) => {
let sec = acc
if (curr[1] === 's') {
sec += parseInt(curr[0])
}
if (curr[1] === 'm') {
sec += parseInt(curr[0]) * 60
}
if (curr[1] === 'h') {
sec += parseInt(curr[0]) * 60 * 60
}
return sec
}, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment