AJJJ
AJJJ

Reputation: 11

convert audio second time to minute and second format

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

Upvotes: 1

Views: 1826

Answers (4)

Sunday Etom
Sunday Etom

Reputation: 341

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

Upvotes: 0

Feathercrown
Feathercrown

Reputation: 2591

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;

Upvotes: 1

phihag
phihag

Reputation: 288140

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));
}

Upvotes: 0

AJJJ
AJJJ

Reputation: 11

        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);

Upvotes: 0

Related Questions