Get the current time in milliseconds using JavaScript
In this tutorial, we are going to learn about how to get the current time in milliseconds using JavaScript.
In JavaScript, we can get the current by calling a getTime()
on the new Date() constructor.
const currentTime = new Date().getTime();
To get the current time in milliseconds, divide the current time with 1000
, because 1second = 1000 milliseconds.
Here is an example:
const currentTime = new Date().getTime();
const milliseconds = currentTime/1000;
console.log(milliseconds);
The /
operator in the above code returns the result of dividing the first operand by the second.
Output:
1665281369182
Similarly, we can also use the Date.now()
method to get the current time in Milliseconds.
Here is an example:
const milliseconds = Date.now();
console.log(milliseconds);
The Date.now() method returns the current time in milliseconds since the epoch (January 1, 1970).