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

后端 未结 15 2027
北海茫月
北海茫月 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:47

    I ran into issues with the above solution. I decided to write it as obtuse as possible. I also use my own "getIntValue" in place of parseInt for extra sanity.

    Just thought other searching might appreciate the update.

    Fiddle

        function convertYouTubeTimeFormatToSeconds(timeFormat) {
    
        if ( timeFormat === null || timeFormat.indexOf("PT") !== 0 ) {
            return 0;
        }
    
        // match the digits into an array
        // each set of digits into an item
        var digitArray      = timeFormat.match(/\d+/g);
        var totalSeconds    = 0;
    
        // only 1 value in array
        if (timeFormat.indexOf('H') > -1 && timeFormat.indexOf('M') == -1 && timeFormat.indexOf('S') == -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60 * 60;
        }
    
        else if (timeFormat.indexOf('H') == -1 && timeFormat.indexOf('M') > -1 && timeFormat.indexOf('S') == -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60;
        }
    
        else if (timeFormat.indexOf('H') == -1 && timeFormat.indexOf('M') == -1 && timeFormat.indexOf('S') > -1) {
            totalSeconds    += getIntValue(digitArray[0]);
        }
    
    
        // 2 values in array
        else if (timeFormat.indexOf('H') > -1 && timeFormat.indexOf('M') > -1 && timeFormat.indexOf('S') == -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60 * 60;
            totalSeconds    += getIntValue(digitArray[1]) * 60;
        }
    
        else if (timeFormat.indexOf('H') > -1 && timeFormat.indexOf('M') == -1 && timeFormat.indexOf('S') > -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60 * 60;
            totalSeconds    += getIntValue(digitArray[1]);
        }
    
        else if (timeFormat.indexOf('H') == -1 && timeFormat.indexOf('M') > -1 && timeFormat.indexOf('S') > -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60;
            totalSeconds    += getIntValue(digitArray[1]);
        }
    
    
        // all 3 values
        else if (timeFormat.indexOf('H') > -1 && timeFormat.indexOf('M') > -1 && timeFormat.indexOf('S') > -1) {
            totalSeconds    += getIntValue(digitArray[0]) * 60 * 60;
            totalSeconds    += getIntValue(digitArray[1]) * 60;
            totalSeconds    += getIntValue(digitArray[2]);
        }
    
    //  console.log(timeFormat, totalSeconds);
    
        return totalSeconds;
    }
    function getIntValue(value) {
        if (value === null) {
            return 0;
        }
    
        else {
    
            var intValue = 0;
            try {
                intValue        = parseInt(value);
                if (isNaN(intValue)) {
                    intValue    = 0;
                }
            } catch (ex) { }
    
            return Math.floor(intValue);
        }
    }
    

提交回复
热议问题