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
let A = Date.now();
console.log( "The time elapsed in millisecond is: " + A);
|
Output
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
let d = Date(Date.now());
a = d.toString()
console.log( "The current date is: " + a)
|
Output
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.
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
Last Updated :
05 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...