Last Updated : Jan 1, 2023
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>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In html we defined h3 tag with concept heading and div tag defined id ‘res’ for appends result on webpage.
- 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’.
- 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.
- Both </body>, </html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
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.