get date in milliseconds javascript
Get Date in Milliseconds using JavaScript
JavaScript provides a built-in Date object that allows you to work with dates and times. To get the current date in milliseconds, you can use the getTime() method attached to the Date object.
Method 1: Using getTime()
// Get current date in milliseconds
const currentDate = new Date().getTime();
// Display date in console
console.log(currentDate);
In the code snippet above, we create a new instance of the Date object and call the getTime() method to get the current date in milliseconds. We then store this value in the currentDate variable and display it in the console using console.log().
Method 2: Using Date.now()
// Get current date in milliseconds
const currentDate = Date.now();
// Display date in console
console.log(currentDate);
The second method to get the current date in milliseconds is by calling the static method now() on the Date object. This method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
Both methods work similarly and return the same result. You can choose any method that suits your coding style and preference.