Get Date in JavaScript (Today, Tomorrow & Yesterday)

In this article we will learn how to get date using JavaScript object. There are various methods that retrieve the date in JavaScript. The data values that can possibly get from a date object in JavaScript are years, months, days, hours, minutes, seconds, and even milliseconds.

Here we will show you the method to get today’s, yesterday’s and tomorrow’s date in JavaScript.

Get today’s date in JS

Method 1

The code that we shared below shows the various methods to get today’s date:

var today = new Date();
console.log("Today is:", today);

The output for this piece of code will be:

Fri Jul 01 2022 16:48:06 GMT+0530 (India Standard Time)

We see that we get today’s date along with day and time. We can use toDateString() method to convert the date into a readable string.

var today = new Date();
today = today.toDateString();
console.log("Today is:" today);

The output for this method will be just the date:

Jul 01 2022

Method 2

Now, let’s see another method to print the present day’s date using JavaScript.

var date=new Date();
var day=date.getDate();
var month=date.getMonth()+1;
var year=date.getFullYear();
console.log(day + "-" + month + "-" + year);

// Output of the code written above is: 1-7-2022

• The getDate() method is used to return the day of a date as a number (1-31).

• The getMonth() method is used to return the month of a date as a number (0- 11).

• The getFullYear() method is used to return the year (in 4-digit) for the specified date according to local time.

Get yesterday’s date in JS

Once we understand how to get the present day’s date, it’s pretty simple to get yesterday’s date. Here first we have to get the present date, then we will use getDate() function to get the day of the date. After subtracting one day in the date, we will use another function called setdate() to set yesterday’s date into the date object.

setDate() function will help us to set the date of a month into it’s date object which were created using date() constructor.

Let’s see the code for the same:

let today = new Date();
let yesterday = new Date(today);

// Output will be as follows: 'Thu Jun 30 2022'

Suppose if we set the date as 33 or any number above 31, the function will subtract 31 from the number to get the date for next month.

Get tomorrow’s date in JS

After the seeing the above example of how to retrieve yesterday’s date, we can easily write the code to get the next day’s date. Instead of subtracting 1 from the date which we did in the last example, here we will add 1 to the present day. It’s as simple as that.

Let’s see the code for the same:

let today = new Date();
let tomorrow = new Date(today);

This code will fetch us the date for the next day (i.e. tomorrow). If new date of the month is set to zero (0), the new date will be set to the last day of the previous month.

Hope the article was insightful and cleared all your queries regarding the date object in JavaScript which will help you in getting the Tomorrow, today, or yesterday date in JavaScript.

Leave a Comment