How to Get the Current Date in Javascript?
There are several ways to get the current date in Javascript:
Using the Date Object:
var today = new Date(); console.log(today);
The
new Date()
function returns the current date and time.Using the Date.now() method:
var today = Date.now(); console.log(today);
The
Date.now()
method returns the current timestamp in milliseconds since January 1, 1970.Using the new Intl.DateTimeFormat() method:
var today = new Intl.DateTimeFormat().format(new Date()); console.log(today);
The
Intl.DateTimeFormat()
method formats the date according to the user's locale.Using the Moment.js library:
var today = moment().format('YYYY-MM-DD'); console.log(today);
The Moment.js library provides a powerful API for handling date and time in Javascript. Here, we're using the
format()
method to specify the output format of the date.