Convert ISO 8601 time format into normal time duration

前端 未结 4 564
隐瞒了意图╮
隐瞒了意图╮ 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:23

    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(":")
    }
    

提交回复
热议问题