7

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

user2091061
  • 879
  • 3
  • 10
  • 32
  • 1
    "PT1M:33S" doesn't look like a valid ISO 8601 date. The closest format would be "YYYY-MM-DDTHH:mm:ss". – Pavlo Sep 30 '13 at 11:41
  • Actually i am fetching youtube videos and i am getting videos's duration as "PT1M33S'. Now i need to convert it into normal time format. – user2091061 Sep 30 '13 at 11:45
  • You could just parse it with a simple regular expression. Or you could see if the value `durationMs` (duration in milliseconds), that the youtube API also provides, suits your needs better … – CBroe Sep 30 '13 at 12:06
  • 2
    For the record, this *is* valid ISO8601 format. See https://en.wikipedia.org/wiki/ISO_8601#Durations – jezmck Sep 08 '17 at 12:50

4 Answers4

19

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.

Praveen
  • 55,303
  • 33
  • 133
  • 164
5

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

kennebec
  • 102,654
  • 32
  • 106
  • 127
2

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(":")
}
brasofilo
  • 25,496
  • 15
  • 91
  • 179
Ben
  • 21
  • 1
0

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
}