Converting hours,minutes into seconds by parsing youtube t= queryparam in javascript/JQuery

I want to parse the timestamp t= in youtube url("http://youtu.be/O4tzHn-EuHc?t=1h5m16s") and calculate the total number of seconds that i need to pass as a start parameter for embed youtube url "http://www.youtube.com/embed/XGSy3_Czz8k?start="

To get the hours, minutes and seconds i use reg-exp's as below. Let me know any improvement can be done in code to make it simple.

var url ="http://youtu.be/XGSy3_Czz8k?t=1h5m16s";
var timeStamp = url.match("t=(.)*?[&|(\s)]");
var hours = timeStamp[0].match(/(\d)+h/); 
var minutes = timeStamp[0].match(/(\d)+m/);
var seconds = timeStamp[0].match(/(\d)+s/);

var totalTimeInSeconds = 0;

if (hours) {
  hours = hours[0].replace("h","");
  totalTimeInSeconds += hours * 60 * 60;
}

if (minutes) {
    minutes = minutes[0].replace("m","");
    totalTimeInSeconds += minutes * 60;
}

if (seconds) {
   seconds = seconds[0].replace("s","")
   totalTimeInSeconds += seconds * 1;
}

console.log("hours:"+hours);
console.log("minutes:"+minutes);
console.log("seconds:"+seconds);
console.log("TotalTimeInSeconds:"+ totalTimeInSeconds);


<iframe width="420" height="345"
 src="http://www.youtube.com/embed/XGSy3_Czz8k?start="+totalTimeInSeconds>
 </iframe>

Upvotes: 3

Views: 343

Answers (3)

Zohaib Waqar
Zohaib Waqar

Reputation: 1239

Try this

var url ="http://youtu.be/XGSy3_Czz8k?t=1h5m16s";
    var timeStamp = url.match("t=(.)*?[&|(\s)]");
    timeStampSplitted = timeStamp[0].replace("t=","").replace("h", ":").replace("m", ":").replace("s", "").split(':');

    // minutes are worth 60 seconds. Hours are worth 60 minutes.
    var seconds = (+timeStampSplitted[0]) * 60 * 60 + (+timeStampSplitted[1]) * 60 + (+timeStampSplitted[2]);

Upvotes: 0

smnbbrv
smnbbrv

Reputation: 24571

I think the easiest is to use RegExp replace function:

var seconds = "1h5m16s".replace(/([0-9]+)h([0-9]+)m([0-9]+)s/, function(match, p1, p2 ,p3) {
  return p1 * 60 * 60 + p2 * 60 + p3 * 1
})

Note p3 * 1 - it is a shortcut for parseInt. Also note that replace will return you a string - don't forget to convert to a number if needed.

Upvotes: 0

Fiona Runge
Fiona Runge

Reputation: 2311

I think a good source for getting comments on your code would be codereview.

You can get rid of your String.replace calls by slightly adjusting your regexes to read like this:

var hours = timeStamp[0].match(/(\d+)h/); 
var minutes = timeStamp[0].match(/(\d+)m/);
var seconds = timeStamp[0].match(/(\d+)s/);

With these regexes you will capture all digits at once, and can than use them like this:

if (hours) {
  totalTimeInSeconds += parseInt(hours[1], 10) * 60 * 60;
}

if (minutes) {
    totalTimeInSeconds += minutes[1] * 60;
}

if (seconds) {
   totalTimeInSeconds += seconds[1];
}

The use of parseInt is not necessary there, but I'd probably introduce it to make it more explicit that conversion is taking place. I'd also suggest adjusting the regex for your timeStamp variable so that it already narrows down on the t parameter more.

Upvotes: 1

Related Questions