Skip to content
Advertisement

How to datetime parse a non-standardized time format

I would like to create datetime objects from a list of string timecodes like these. However, parse interprets incorrectly for my use case.

JavaScript

The list above comes from YouTube’s transcript timecodes. When copied from the site, they use a variable format to designate hours, minutes, and time, based on elapsed time:

JavaScript

and parse results in (comments are my interpretations):

JavaScript

i.e. if a string doesn’t consist of a full timecode including hours, minutes, seconds, then parse appears to think that minutes are hours, and seconds are minutes.

How can I either dynamically parse the list to default interpretation to minutes & seconds instead of hours & minutes, or alternatively adjust the timecodes intelligently so that they conform to the parse format?

Advertisement

Answer

This is a little tricky but should work:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement