All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Get Current Time In JavaScript

Last Updated : Mar 11, 2024

Get Current Time In JavaScript

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.

<title> Get current time in javascript
    <div id="currentTime"></div>
      const element = document.getElementById('currentTime');
      setInterval(function () {
        const currentDate = new Date();
        element.innerText = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
      }, 1000);
  1. Our code begins with HTML and HEAD tags.
  2. The TITLE tag is then used to add the page's title, and the title and code are thus finished.
  3. With the BODY tag, we then begin the code's body, and we utilise the div's id as the currentTime after that.
  4. As soon as that is done, we begin the script in which we use the document.getelementbyid for the element's current time.
  5. The current playback time is specified in seconds using the currentTime property of the HTMLMediaElement interface.
  6. The setinterval function is then used, and it is A function is called at predetermined intervals by the setInterval() method (in milliseconds).
  7. 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.
  8. 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.
  9. 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.
  10. Next, we use the JavaScript date function currentDate.getSeconds. The getSeconds() method delivers the local time-adjusted number of seconds for the supplied date.
  11. 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.

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 🡪