Convert ISO 8601 time format into normal time duration

前端 未结 4 529
隐瞒了意图╮
隐瞒了意图╮ 2021-01-12 15:36

I have a duration string \"PT1M33S\". I would like to get result in the following format -> 01:33 Can anyone please tell me how to do so using js or jquery??

相关标签:
4条回答
  • 2021-01-12 16:20

    This seems be not a timeformat, just duration of the video.

         ------ 33 Seconds
        ''
    PT1M33S
      '------- 1 Minute
    

    H - Hours
    M - Minutes
    S- Seconds

    So try this

    var timeD = "PT1M33S";
    var formattedTime = timeD.replace("PT","").replace("H",":").replace("M",":").replace("S","")
    alert(formattedTime);
    

    Fiddle for example, can be a done with a simple regex too.

    Hope you understand.

    0 讨论(0)
  • 2021-01-12 16:22

    I liked this answer as it was simple, however, it yields strange results if the minutes or seconds are less than 10, for example, 10:04 is returned as 10:4.

    So I added some simple functions to further parse and re-assemble the time string:

    function formatTimeSeg(segment) {
        newSegment = segment;
        segLength = segment.length;
        if(segLength==1){
            newSegment = '0'+segment;
        }
    return newSegment;
    }
    extractedTime = duration.replace("PT","").replace("H",":").replace("M",":").replace("S","")
    extractedTime = extractedTime.split(':');
    timeLength = extractedTime.length;
    switch(timeLength) {
        case 3:
            hours=extractedTime[0];minutes=extractedTime[1];seconds=extractedTime[2];
            minutes = formatTimeSeg(minutes);
            seconds = formatTimeSeg(seconds);
            formattedTime = hours+':'+minutes+':'+seconds;
        break;
        case 2:
            minutes=extractedTime[0];seconds=extractedTime[1];
            minutes = formatTimeSeg(minutes);
            seconds = formatTimeSeg(seconds);
            formattedTime = minutes+':'+seconds;
        break;
        case 1:
            seconds=extractedTime[0];
            seconds = formatTimeSeg(seconds);
            formattedTime = '0:'+seconds;
        break
    }
    
    0 讨论(0)
  • 2021-01-12 16:23

    You can match the digits and pad them to format-

    var string= "PT1M33S", 
    array=string.match(/(\d+)(?=[MHS])/ig)||[]; 
    
    var formatted=array.map(function(item){
        if(item.length<2) return '0'+item;
        return item;
    }).join(':');
    
    formatted
    

    /* returned value: (String) 01:33 */

    0 讨论(0)
  • I personally use this:

    function parseDuration(e){var n=e.replace(/D|H|M/g,":").replace(/P|T|S/g,"").split(":");if(1==n.length)2!=n[0].length&&(n[0]="0"+n[0]),n[0]="0:"+n[0];else for(var r=1,l=n.length-1;l>=r;r++)2!=n[r].length&&(n[r]="0"+n[r]);return n.join(":")}
    

    Same code formatted:

    function parseDuration(e){
        var n = e.replace(/D|H|M/g,":").replace(/P|T|S/g,"").split(":");
    
        if(1 == n.length)
            2!=n[0].length && (n[0]="0"+n[0]),n[0]="0:"+n[0];
        else 
            for(var r=1, l=n.length-1; l>=r;r++)
                2!=n[r].length && (n[r]="0"+n[r]);
    
        return n.join(":")
    }
    
    0 讨论(0)
提交回复
热议问题