Get Current Time In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Anjali
In this article we will show you the solution of get current time in JavaScript, a built-in object called Date is available in JavaScript to handle actions involving all dates and times.
It can be used to display the time and date as well as to design timers and calendars.
A Date object can be used by many different methods once it has been generated.
You can retrieve and set the object's year, month, day, hour, minute, second, and millisecond properties using the majority of them. Now move to the concept of get current time in JavaScript.
Step By Step Guide On Get Current Time In JavaScript :-
A built-in object called Date is available in JavaScript and assists in managing all date and time information.
In JavaScript, the current time is obtained via the Date object. We should utilise a setInterval JavaScript built-in method to update time in order to show that current time on the HTML page.
<html> <head> <title> Get current time in javascript </title> </head> <body> <div id="currentTime"></div> <script> const element = document.getElementById('currentTime'); setInterval(function () { const currentDate = new Date(); element.innerText = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds(); }, 1000); </script> </body> </html>
- Our code begins with HTML and HEAD tags.
- The TITLE tag is then used to add the page's title, and the title and code are thus finished.
- With the BODY tag, we then begin the code's body, and we utilise the div's id as the currentTime after that.
- As soon as that is done, we begin the script in which we use the document.getelementbyid for the element's current time.
- The current playback time is specified in seconds using the currentTime property of the HTMLMediaElement interface.
- The setinterval function is then used, and it is A function is called at predetermined intervals by the setInterval() method (in milliseconds).
- Then, thus, we use the new Date object A new Date() object in JavaScript makes it simple to obtain the current time or date. It employs the time zone of our browser by default.
- Next, we employ currentDate.getHours, a date in Javascript The getHours() method returns, in local time, the hour in the supplied date. getHours() returns an integer among 0 and 23 as its value.
- Next, we employ currentDate. getMinutes, a Javascript date function The getMinutes() method returns, in local time, the number of minutes in the supplied date. getMinutes() returns an integer with a range of 0 to 59.
- Next, we use the JavaScript date function currentDate.getSeconds. The getSeconds() method delivers the local time-adjusted number of seconds for the supplied date.
- Finally, we use the SCRIPT, BODY, and HTML elements to wrap up our code.
Conclusion :-
So, we have successfully mastered the JavaScript notion of getting the current time.
We also learnt how to obtain the current time and date and apply it to our requirements in a simple, meaningful, and practical manner that even a complete novice can comprehend and employ.
I hope this article on get current time in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.