How To Get Current Time In JavaScript

Published On - 7 May 2024

In this article we will show you the solution of how to get current time in javascript, in javascrript, we use the methods such as Date() object, getTime(), getHours(), and getMinutes() used for getting the time in hours, minutes, and seconds.

And toLocaleTimeString() method to get the localized time means the time according to a specific region or country and using the toUTCString() method to get the time in UTC format and using the getTime() method to get the time in milliseconds.

And setTimeout() to update the time every second.

Step By Step Guide On How To Get Current Time In JavaScript :-

1, Using Date() object

const my_time= new Date();
console.log(my_time); // 2023-08-19T22:09:15.591Z
  1. We create a variable named my_time and assign a new Date() object.
  2. Use console.log() to print my_time.

2. Using getTime(),getHours() and getMinutes() methods

const my_time= new Date();
const in_hours= my_time. getHours();
const in_min = my_time.getMinutes();
const in_sec= my_time.getSeconds();
const res= in_hours +":" + in_min + ":" + in_sec;
console.log(res); // 22:24:11
  1. We create a variable named my_time and assign a new Date() object and use getHours(), getMinutes(), and getSeconds() methods is used for getting the time in hours, minutes, and seconds and assign in a variable in_hours, in_min, and in_sec.
  2. After that we can concatenate these variables to print the current time in the correct format and assign in a variable res.
  3. Use console.log() to print res.

3. Using toLocaleTimeString() method

const my_time= new Date();
const a= my_time.toLocaleTimeString();
console.log(a); // 10:31:47 PM
  1. We initliaze a variable my_time and create new Date() object.
  2. We can use the toLocaleTimeString() method to get the localized time and assign it in a variable a.
  3. Use console.log() to print a.

4. Using toUTCString() method

const my_time= new Date();
const a= my_time.toUTCString();
console.log(a); // Sat, 19 Aug 2023 22:37:31 GMT
  1. We initliaze a variable my_time and create new Date() object.
  2. We can use the toUTCString() method to get the time in Coordinated universal time and assign it in a variable a.
  3. Use console.log() to print a.

5. Using getTime() method

const my_time= new Date();
const a= my_time.getTime();
console.log(a); // 1692484927011
  1. We initliaze a variable my_time and create new Date() object.
  2. getTime() method is used for getting the time in milliseconds.
  3. Use console.log() to print a.

6. Use setTimeout() to update time in every seconds

function updateTime(){
    const my_time= new Date();
    const a= my_time.toUTCString();
    console.log(a);
setTimeout(updateTime, 1000);
}
updateTime();
  1. We create a function named updateTime().
  2. Within the function, we initialize a variable my_time and create a new Date() object.
  3. Use console.log to print a.
  4. We use the setTimeout() method and pass the updateTime() function and 1000 milliseconds or 1 second or we can say updateTime() function calls in every 1 second. Every 1 second time will be updated.

Conclusion :-

In conclusion, we successfully learned how to get current time in javascript.

We learned methods such as using the Date() object, using getHours, getMinutes(), and getSeconds(), using the getTime() method, using toLocaleTimeString and toUTCString(), and finally setTimeout().

These all methods generate current time in javascript. All methods generate time in a different manner. According to your’s need, you can choose any one of the mentioned methods.

I hope this article on how to get current time in javascript helps you and the steps and method mentioned above are easy to follow and implement.