Date now() Method UseCases
JavaScript `getDate()` method retrieves the day of the month (1-31) from a Date object. It doesn’t require any parameters and returns the numeric value representing the day of the month.
To log the current date in JavaScript, create a new Date object and call its toDateString() method, or use console.log(new Date().toDateString()).
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article
Supported Browsers
JavaScript Date now() Method
The Date.now()
method in JavaScript returns the current timestamp in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 UTC). Date.now() is a static method of the Date object.
Date now() Method Syntax
let A = Date.now();
Date now() Method Parameters
This method does not accept any parameter.
Date now() Method Return Values
It returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.
Date now() Method Example
The below example illustrates the Date now() method.
Javascript
// Use of method Date.now() let A = Date.now(); // Printing the number of millisecond elapsed console.log( "The time elapsed in millisecond is: " + A); |
The time elapsed in millisecond is: 1702447051020
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.
Date now() Method Example:
This example illustrates getting the current date using the Date.now() method.
Javascript
// 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) |
The current date is: Wed Dec 13 2023 05:57:31 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.