Search code examples
javascripthtmlvideotimestamphtml5-video

How to get video timestamp in HTML


I am working on one project in which I need to show the timestamp of video in actual time format as "01:12:50". Below is my code which return the current time of video as frame rate ie "12.526". Can anyone help me how I can change it to actual time or is there any way that I can directly get the video timestamp using HTML and JavaScript.

<video id="myVideo" width="740px" height="600px"></video>
<div id="timer"> </div>

<script>
    document.getElementById("myVideo").addEventListener('timeupdate', function() {
        document.getElementById("timer").innerHTML = this.currentTime;
        currentTime = this.currentTime;
    });
</script>

Solution

  • You can just do a simple math!

    document.getElementById("myVideo").addEventListener('timeupdate', function() {
        var hours=parseInt(video.currentTime/(60*60),10);
        var minutes = parseInt(video.currentTime / 60, 10);
        var seconds = video.currentTime % 60;
        if (hours==0) { 
            documentgetElementById("timer").innerHTML=minutes+":"+seconds.toFixed(0)
        } else { 
            document.getElementById("timer").innerHTML=hours+":"+minutes+":"+seconds.toFixed(0)
        }
    });
    

    You may or may not need if else code. You can change as you want.