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.
First contains hours, second contains minutes, last contains seconds.
Hours will be empty if no hours are found.