title: Converting Youtube Data API V3 video duration format to seconds in JavaScript/Node.js
body: <p>I'm trying to convert ISO 8601 string to seconds in JS/Node. The best I could come up with was:</p>
  
  <pre><code>function convert_time(duration) {
      var a = duration.match(/\d+/g)
      var duration = 0
  
      if(a.length == 3) {
          duration = duration + parseInt(a[0]) * 3600;
          duration = duration + parseInt(a[1]) * 60;
          duration = duration + parseInt(a[2]);
      }
  
      if(a.length == 2) {
          duration = duration + parseInt(a[0]) * 60;
          duration = duration + parseInt(a[1]);
      }
  
      if(a.length == 1) {
          duration = duration + parseInt(a[0]);
      }
      return duration
  }
  </code></pre>
  
  <p>It works when I input strings such as "PT48S", "PT3M20S" or "PT3H2M31S", but fails miserably if the string is "PT1H11S". Does anyone have a better idea?</p>