• Post category:JavaScript
  • Reading time:3 mins read

We’ll show you how to retrieve the date from yesterday, today, and tomorrow using JavaScript in this brief tutorial. In JavaScript, we can quickly calculate the date by using the Date object.

A built-in Date object in JavaScript represents the current date and time. The current date (today) will be obtained first, followed by the dates of yesterday and tomorrow.

Get today’s, tomorrow’s and yesterday’s date using JavaScript

  1. Get today’s date
  2. Get tomorrow’s date
  3. Get yesterday’s date

1. Get today’s date

As we mentioned, we will get today’s date using the date object as shown below in the code. We will use the toDateString() method to convert the date into a readable string.

let dt = new Date();
// Wed Apr 06 2022 00:40:18 GMT+0530 (India Standard Time)

dt = dt.toDateString();
// Wed Apr 06 2022

2. Get tomorrow’s date

To calculate tomorrow’s date, first we have to get the current date (today) then add 1 day in the current date.

let today = new Date();
let tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
// Thu Apr 07 2022

Here, we have used the getDate() function to get the day of the date. After adding one day in the date, use the setDate() function to set tomorrow’s date into the date object.

3. Get yesterday’s date

Same way, we can easily calculate the yesterday’s date. Here, we will subtract 1 day from the date.

let today = new Date();
let yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
// Tue Apr 05 2022

I hope you find this article is helpful.
Thank you for reading. Happy Coding..!! 🙂

Leave a Reply