All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Get Current Time In Milliseconds

Last Updated : Jul 1, 2023

JavaScript Get Current Time In Milliseconds

In this tutorial we will show you the solution of JavaScript get current time in milliseconds, here we defined date object and using getTime() method we gets current time in milliseconds format.

For collecting current time in millisecond format we had another options also in javascript (i.e) valueOf() method will returns the same result so the result will not separated as hour, minute, seconds or milliseconds.

Step By Step Guide On JavaScript Get Current Time In Milliseconds :-

Here we defined h3 tag with concept heading and div tag defined with id ‘res’ for appends time result on webpage.

In script we defining date object ‘t’ with that we adding getTime() method.

The getTime() method returns the numeric value corresponding to the time for the specified date according to universal time and this is number of milliseconds since 1 jan.

Finally we appended result string on div element ‘res’ by innerHTML.

<!DOCTYPE html>
<html>
    <head>
        <title>TIME IN MSecds</title>
    </head>
    <body>
        <h3>TIME IN MILLISECONDS</h3>
        <div id="res"></div>
        <script>
            var t=new Date().getTime();
            document.getElementById('res').innerHTML=t;
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In html we defined h3 tag with concept heading and div tag defined id ‘res’ for appends result on webpage.
  7. In <script> we defined date object its referred by variable ‘t’ then we adding getTime() method with that for gets current time in milliseconds and finally we appended result on div element ‘res’.
  8. So when we refresh our result will get updated for each milliseconds in browser window and also its look like an long integer we can’t see hour, minute, second separately like in normal format.
  9. Both </body>, </html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get current time in milliseconds format using javascript.

When we executing this program on browser we can see result of milliseconds format current time will displayed on webpage with heading ‘TIME IN MILLISECONDS’.

We can also get current or specified date and time in different formats by using other countries code in toLocaleString() method so try all country codes for your clarification because later we have to use them at somewhere.

I hope this tutorial on JavaScript get current time in milliseconds helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪