The output now is: The youtube video Title - PT1H2M11S
How can I convert this duration to a human readable format in seconds using Javascript?
I tried:
$(a).after("<br />" + b.items[0].snippet.title + " - " + b.items[0].contentDetails.duration..replace("PT","").replace("H",":").replace("M",":").replace("S","") + "" );
But fails when duration is for example: PT4M9S then it outputs as 3:9 when it should be 3:09
function checkLink() {
$("a.link").filter(function () {
if ($(this).attr("href").match("(http|https)://(www.|m.)?youtube|youtu.be")) {
youtube_id = $(this).attr("href").match(/^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/)[2];
$(this).html('<p style="text-align:center;"><img src="https://i.ytimg.com/vi/' + youtube_id + '/mqdefault.jpg" /></p>');
var a = $(this).find("img");
$.ajax({
url: "https://www.googleapis.com/youtube/v3/videos?id=" + youtube_id + "&key=**********************&fields=items(snippet(title),contentDetails)&part=snippet,contentDetails",
dataType: "json",
success: function (b) {
$(a).after("<br />" + b.items[0].snippet.title + " - " + b.items[0].contentDetails.duration + "" );
}
});
return $(this).attr("href", "https://www.youtube.com/embed/" + youtube_id + "?autoplay=1")
}
}).colorbox({
iframe: !0,
innerWidth: 640,
innerHeight: 425
});
};