In Javascript, the .now()
method is a Date static method that returns the number of milliseconds since January 1, 1970 00:00:00 UTC. This date is also known as the UNIX epoch.
Date.now()
There are no parameters.
The Date.now()
method returns a number that represents the milliseconds that have passed since the UNIX epoch up until now.
In the example below, we can see the use of the Date.now()
method and logout the returned value to the console.
// use the Date.now() methodlet date = Date.now()// log returned value to consoleconsole.log(date);
Note: The returned value of the
Date.now()
can be passed to a date object.
In the code below, we use the returned milliseconds of Date.now()
as a parameter to a date object. Next, we format the date object to an interpretable format.
// use the Date.now() methodlet date = Date.now()// use milliseconds in a date objectlet currentDate = new Date(date)// log milliseconds value to consoleconsole.log(date);// format the date objectconsole.log("Current time is :" +currentDate.toString());