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??
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
}