I am analyzing timestamped YouTube comments. Because some comments may refer to a period in either mm:ss, m:ss, hh:mm:ss, or h:mm:ss, I need to prepare for these cases. The following code works on mm:ss and m:ss, but still treats the one with hours as if it was mm:ss. For example, 02:24:30 returns 144, as it is only analyzing the first two parts. Here is the code:
var timePattern = /(([0-5][0-9])|[0-9])\:[0-9]{2,2}/;
var seconds = "";
for (var i = 0; i < comments.length; i++) {
var matches = comments[i].match(timePattern);
var matched = matches[0];
var a = matched.split(':');
if(matched.length == 7 || matched.length == 8) {
seconds = (+a[0])*60*60 + (+a[1])*60 + a[2];
} else {
seconds = (+a[0])*60 + (+a[1]);
}
times.push(seconds);
}
Try a different regex.
(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])
First contains hours, second contains minutes, last contains seconds.
Hours will be empty if no hours are found.
You can also get this info on the comments feed, if you're using the API: https://developers.google.com/youtube/2.0/developers_guide_protocol#Comments
The comments feed have a published
element that contains the date and time information that you can use to parse the seconds of.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With