1
var currentTime = audio.currentTime | 0;
var duration = audio.duration | 0;

it works but, it shows the audio's total length and current time in only second format i want to convert the default second value in Minute:Second format

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
AJJJ
  • 11
  • 2
  • 1
    Possible duplicate of [Javascript seconds to minutes and seconds](http://stackoverflow.com/questions/3733227/javascript-seconds-to-minutes-and-seconds) – lamp76 May 02 '16 at 11:44

4 Answers4

1

Try this (lightly tested):

var seconds = currentTime % 60;
var foo = currentTime - seconds;
var minutes = foo / 60;
if(seconds < 10){
    seconds = "0" + seconds.toString();
}
var fixedCurrentTime = minutes + ":" + seconds;
Feathercrown
  • 2,547
  • 1
  • 16
  • 30
0
        var currentTime = audio.currentTime | 0;                

        var duration = audio.duration | 0;          

        var minutes = "0" + Math.floor(duration / 60);
        var seconds = "0" + (duration - minutes * 60);
        var dur = minutes.substr(-2) + ":" + seconds.substr(-2);


        var minutes = "0" + Math.floor(currentTime / 60);
        var seconds = "0" + (currentTime - minutes * 60);
        var cur = minutes.substr(-2) + ":" + seconds.substr(-2);
AJJJ
  • 11
  • 2
  • This duplicates the code where a function should be used. In addition, the output for 7425 will be `23:25`, where it should be `123:25`. In any case, put the code into a function so you don't duplicate it. – phihag May 02 '16 at 11:48
0

You can simply write the code yourself; it's not as if it's complicated or would ever change:

function pad(num, size) {
    var s = num + '';
    while (s.length < size) {
       s = '0' + s;
    }
    return s;
}

function format_seconds(secs) {
    return Math.floor(secs / 60) + ':' + (pad(secs % 60, 2));
}
phihag
  • 278,196
  • 72
  • 453
  • 469
0

dropping my own answer after 5 years and 9 months.

function() {

  if(this.myAudio.readyState > 0) {

    var currentTime = this.myAudio.currentTime;
    var duration = this.myAudio.duration;

    var seconds: any = Math.floor(duration % 60);
    var foo = duration - seconds;
    var min: any = foo / 60;
    var minutes: any = Math.floor(min % 60);
    var hours: any = Math.floor(min / 60);

    if(seconds < 10){
      seconds = "0" + seconds.toString();
    }

    if(hours > 0){
      this.audioDuration = hours + ":" + minutes + ":" + seconds;
    } else {
      this.audioDuration = minutes + ":" + seconds;
    }
    
  }

}

I used typescript, hope this helps...

Sunday Etom
  • 171
  • 1
  • 5