Last Updated : Jul 1, 2023
For collecting 24hours format we used British country code ‘en-GB’ and toLocaleString() method used to get current date and time then using slice we removed date from string time only displayed as result.
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 collecting british country current date and time by toLocalString(‘en-GB’) method and date, time string stored on variable ‘re’.
Using slice() method we removing date part from string by value ’12-refers length of letters’ and result updated on variable ‘re’.
Finally we appended result string on div element ‘res’ by innerHTML.
<!DOCTYPE html> <html> <head> <title>TIME IN 24hrs</title> </head> <body> <h3>TIME IN 24HRS FORMAT</h3> <div id="res"></div> <script> var t=new Date(); var re=t.toLocaleString('en-GB'); re=re.slice(12); document.getElementById('res').innerHTML=re; </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 for collecting current date time we used ‘t’ with ‘toLocaleString(‘en-GB’)’ method so result of british country date time format stored on variable ‘re’.
- We need only time part so we removed date part from that string using slice method and updated result stored on same variable ‘re’ finally we appended result on div element ‘res’.
- 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 24 hours format current time will displayed on webpage with heading ‘TIME IN 24HRS FORMAT’.
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.