Convert ISO 8601 time format into normal time duration

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

    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 */

提交回复
热议问题