How to Get the Current Date in Javascript?

javascript

There are several ways to get the current date in Javascript:

  1. Using the Date Object:

    var today = new Date();
    console.log(today);
    

    The new Date() function returns the current date and time.

  2. 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.

  3. 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.

  4. 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.

Latest Questions

javascript How to Check if a Javascript Array Includes a Given Value? javascript How to Completely Uninstall Nodejs, and Reinstall From Scratch on Mac OS X? javascript How to Map Values of a Javascript Object to a New Object Using a Function?