Difference between date now and new date in js

In JavaScript, Date.now() and new Date() are two different ways to get the current date and time. Here's a breakdown of each:

1. Date.now()

Date.now() returns the number of milliseconds that have elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC). This method is also known as the "timestamp" or "Unix timestamp".

Example: console.log(Date.now()); // Output: 1643723400000

Pros:

Cons:

2. new Date()

new Date() creates a new Date object, which represents the current date and time. This method returns a Date object with the current date and time, including the year, month, day, hour, minute, second, and millisecond.

Example: console.log(new Date()); // Output: Fri Feb 18 2022 14:30:00 GMT+0000 (Coordinated Universal Time)

Pros:

Cons:

When to use each:

I hope this helps clarify the difference between Date.now() and new Date() in JavaScript!