You are currently viewing How to get current time in JavaScript? Quick Answer

How to get current time in JavaScript? Quick Answer

To get the current time in JavaScript, you can use the Date object and its getTime method:

let currentTime = new Date().getTime();

This will give you the current time in milliseconds since the Unix epoch (midnight on January 1, 1970).

If you want to get the current time in a specific format, you can use the toLocaleTimeString method of the Date object:

let currentTime = new Date().toLocaleTimeString();

This will give you a string representation of the current time in the user’s local time zone, using the browser’s default time format.

You can also specify a custom time format by passing a format string as an argument to the toLocaleTimeString method:

let currentTime = new Date().toLocaleTimeString('en-US', {hour: '2-digit', minute: '2-digit'});

This will give you a string representation of the current time in the format “hh:mm” (e.g., “09:30”).

You can find more information about the Date object and its methods in the documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Join us on Facebook

Read more about programming

Leave a Reply