Time divided into minutes and seconds

I have the following code that should 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 problem 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 do not know how to use the first code to, for example, currentTimeappear as follows: minutes:seconds.

Any help please?

+4
source share
4 answers

With a little commit, you can leave this as:

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));
});
+1
source

You can simply pass values ​​like the video[0].currentTimefunction fromSecondsthat 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));
});
+1
source
 $('#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 
 });
0

, currentTime - , .

fromSeconds , fromSeconds(mytimevalue) mm:ss :

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

- JavaScript Date() Object, : var currentTime = new Date(video[0].currentTime * 1000);

Then you can use Date.getMinutes()and Date.getSeconds()to find your values.

More here

0
source

All Articles