user4850448
user4850448

Reputation:

Get time split into minutes and seconds

I have the following code, which is supposed to do what I need:

function fromSeconds(seconds, showHours = false) {
    if(showHours) {
        var hours = Math.floor(seconds / 3600),
            seconds = seconds - hours * 3600;
    }
    var minutes = (Math.floor(seconds/60) < 10) ? 
        "0" + Math.floor(seconds/60) : Math.floor(seconds/60);
    var seconds = (seconds % 60 > 9) ? seconds % 60 : "0" + seconds % 60;

    if(showHours) {
        var timestring = hours + ":" + minutes + ":" + seconds;
    } else {
        var timestring = minutes + ":" + seconds;
    }
    return timestring;
}

The problems is that I also have this:

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function() {
    $('#currentTime').html(video[0].currentTime.toFixed(2));
    $('#remTime').html((video[0].duration - video[0].currentTime).toFixed(2));
    $('#totalTime').html(video[0].duration.toFixed(2));
});

And I don't know how to apply the first code so that for example currentTime is displayed like this: minutes:seconds.

Any help please?

Upvotes: 4

Views: 1437

Answers (4)

Shekhar Pankaj
Shekhar Pankaj

Reputation: 9145

 $('#currentTime').html(function(){
     var time=video[0].currentTime.toFixed(2);
     //some conversion needed in-order to convert to required format
     fromSeconds(time,false)//returns time 
 });

Upvotes: 0

Maciej Kwas
Maciej Kwas

Reputation: 6459

With a small fixation you can leave this as that:

Demo

function fromSeconds(seconds, showHours) {
    if(showHours) {
        var hours = Math.floor(seconds / 3600),
            seconds = seconds - hours * 3600;
    }
    var minutes = ("0" + Math.floor(seconds/60)).slice(-2);
    var seconds = ("0" + parseInt(seconds%60,10)).slice(-2);

    if(showHours) {
        var timestring = hours + ":" + minutes + ":" + seconds;
    } else {
        var timestring = minutes + ":" + seconds;
    }
    return timestring;
}

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function () {
    $('#currentTime').html(fromSeconds(video[0].currentTime));
    $('#remTime').html(fromSeconds(video[0].duration - video[0].currentTime));
    $('#totalTime').html(fromSeconds(video[0].duration));
});

Upvotes: 1

jymbob
jymbob

Reputation: 488

Assuming currentTime is the time in seconds, you need to pass the value into your function.

fromSeconds returns the text your require, so fromSeconds(mytimevalue) will return mm:ss as required:

video.bind("timeupdate", function() {
    $('#currentTime').html( fromSeconds(video[0].currentTime) );
    $('#remTime').html( fromSeconds(video[0].duration - video[0].currentTime) );
    $('#totalTime').html( fromSeconds(video[0].duration) );
});

Another option would be to use JavaScript's Date() Object, which takes milliseconds as a value: var currentTime = new Date(video[0].currentTime * 1000);

You can then use Date.getMinutes() and Date.getSeconds() to find your values.

More details here

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388446

You can just pass the values like video[0].currentTime to the function fromSeconds which will return the formatted string

var video = $('#home_explainer_placeholder');
video.bind("timeupdate", function () {
    $('#currentTime').html(fromSeconds(video[0].currentTime));
    $('#remTime').html(fromSeconds(video[0].duration - video[0].currentTime));
    $('#totalTime').html(fromSeconds(video[0].duration));
});

Upvotes: 1

Related Questions