Converting Youtube Data API V3 video duration format to seconds in JavaScript/Node.js

后端 未结 15 2047
北海茫月
北海茫月 2020-12-05 07:09

I\'m trying to convert ISO 8601 string to seconds in JS/Node. The best I could come up with was:

function convert_time(duration) {
    var a = duration.match         


        
15条回答
  •  孤城傲影
    2020-12-05 07:42

    I realize eval is unpopular, but here's the easiest and fastest approach I can imagine. Enjoy.

    function formatDuration(x) {
       return eval(x.replace('PT','').replace('H','*3600+').replace('M','*60+').replace('S', '+').slice(0, -1));
    }
    

提交回复
热议问题