How to Get Today’s Date in JavaScript

Not sure how to get today’s date and time in JavaScript? This guide will walk you through the process step by step and with code samples.

Published Categorized as JavaScript, Web & Code

To get today’s date and the current time in JavaScript, use the Date() constructor.

For example, the code snippet below constructs a date with the Date() constructor, stores it in a let variable, then outputs it to the browser’s developer console with the console.log() method.

// Construct a date and store it in a variable
let today = new Date();

// Reference the date and output it to the console
console.log(today);

Go ahead and give this code snippet a try. To do so, fire up your browser’s developer console, copy and paste the code in, then hit the Enter key on your keyboard. If you’re reading this from your phone or tablet, here’s a screenshot of my console:

The newly-created Date object holds the date and the time at the moment of instantiation.

Working With the Date Object

Once you’ve obtained today’s date and stored it as a Date object in a constant or variable, you can use a number of JavaScript methods to extract bits and pieces of the data in it.

As a developer, whether front or backend, it’s very useful to learn how to extract data from the Date object. Once you’ve mastered this, you will be able to perform calculations and implement conditional logic in your JavaScript application.

Extract the Day of the Week From the Date Object

To get the day of the week from the date object, use the Date.getDay() method.

// Construct a date and store it in a variable
let today = new Date();

// Get the day of the week and output it to the console
console.log(today.getDay());

The Date.getDay() method returns a number for the day of the week:

  • 0 for Sunday
  • 1 for Monday
  • 2 for Tuesday
  • 3 for Wednesday
  • 4 for Thursday
  • 5 for Friday
  • 6 for Saturday

Pay attention to the response of the method. It’s a common mistake for developers to expect Sunday to have a value of 7 when it should really have a value of 0.

Extract the Month of the Year From the Date Object

To get the month from the date object, use the Date.getMonth() method.

// Construct a date and store it in a variable
let today = new Date();

// Get the month of the year and output it to the console
console.log(today.getMonth());

The Date.getMonth() method returns a number for the month of the year:

  • 0 for January
  • 1 for February
  • 2 for March
  • 3 for April
  • 4 for May
  • 5 for June
  • 6 for July
  • 7 for August
  • 8 for September
  • 9 for October
  • 10 for November
  • 11 for December

Pay attention to the response of the method. It’s a common mistake for developers to expect the months to have values from 1 to 12 instead of 0 to 11.

Extract the Year From the Date Object

To get the month from the Date object, use the Date.getFullYear() method.

// Construct a date and store it in a variable
let today = new Date();

// Get the month of the year and output it to the console
console.log(today.getFullYear());

The Date.getFullYear() method returns a numeric value for the year (for example, 2022).

Note: Some tutorials from a few years ago tell you to use the Date.getYear() method instead. This method is deprecated and should no longer be used in your code, as it may cause browser support problems in the future.

Convert the Date to Milliseconds

To convert the date and time from the Date object into the number of milliseconds elapsed before/after 00:00:00 UTC on January 1st, 1970 (also known as the ECMAScript epoch), use the Date.parse() method.

// Construct a date and store it in a variable
let today = new Date();

// Get the month of the year and output it to the console
console.log(Date.parse(today));

Notice the syntax. The constant or variable containing the date object must be passed as an argument to the Date.parse() method.

By Dim Nikov

Content creator with marketing and web development skills to pay the bills.

Leave a comment

Your email address will not be published. Required fields are marked *