JavaScript Date now() Method
The Date.now() method in JavaScript returns the current timestamp in milliseconds since January 1, 1970. This method doesn’t require creating a new date object, making it one of the fastest and most efficient ways to capture the current time in your code.
Syntax
let curr_date = Date.now();
Parameters
- This method does not accept any parameter.
Return Values
- It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
Different Examples of Date now() Method
Example 1: The below example of the Date now() method.
// Use of method Date.now()
let A = Date.now();
// Printing the number of millisecond elapsed
console.log("The time elapsed in millisecond is: " + A);
Output
The time elapsed in millisecond is: 1720904397838
Explanation:
The code snippet utilizes `Date.now()` to retrieve the current timestamp in milliseconds since the Unix epoch. It stores this value in variable `A` and prints it, indicating the elapsed milliseconds since the epoch.
Example 2: This example of getting the current date using the Date.now() method.
// Use of Date.now() method
let d = Date(Date.now());
// Converting the number of millisecond in date string
a = d.toString()
// Printing the current date
console.log("The current date is: " + a)
Output
The current date is: Sat Jul 13 2024 20:59:57 GMT+0000 (Coordinated Universal Time)
Explanation:
The code snippet generates a new Date object using the current timestamp from Date.now(). It then converts this date object to a string format using .toString() and logs it, displaying the current date and time.
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article