DEV Community

Cover image for Get Yesterday Date in JavaScript
Gaël Thomas
Gaël Thomas

Posted on • Originally published at herewecode.io

Get Yesterday Date in JavaScript

Learn to remove one day-to-date in JavaScript using the native date object.

How to Get Yesterday Date in JavaScript?

The best way to get yesterday’s date in JavaScript is by using the Date object. On an existing Date, you can use the getDate function to get the day (between 1 and 31), subtract 1 to this number, then use setDate to update the date. Here's a short example: date.setDate(date.getDate() - 1).

Using the JavaScript Date Object

In JavaScript, you’ll often manipulate the Date object to do diverse operations (e.g., subtract days from a date, format a date, etc.).

If you want to get yesterday’s date in JavaScript, you can use a combination of these two Date functions:

  1. yourDate.getDate(): get the day of the month (you'll get a number between 1 and 31)

  2. yourDate.setDate(): update the day of the date to the number passed as a parameter

In practice, let’s say you have this date: 15/11/2022. The getDate() function will return 15. Then you can subtract one day to this number 15 - 1 = 14. Finally, use setDate() to update the day of the month for your current date object.

Here’s a commented example:

// Create a date
const todayDate = new Date()

// Before subtracting 1 day
console.log(todayDate.toString())
// Output: "Tue Nov 15 2022 13:37:12 GMT+0100 (Central European Standard Time)"

// Subtract one day to the current date
todayDate.setDate(todayDate.getDate() - 1)

// After removing 1 day
console.log(todayDate.toString())
// Output: "Mon Nov 14 2022 13:37:12 GMT+0100 (Central European Standard Time)"
Enter fullscreen mode Exit fullscreen mode

Now that you know how to subtract days from a date, you can learn how to add one day to date in JavaScript.

Bonus: Create a Function to Get Yesterday Date

Now you know how to remove days to a date, let’s make your code fancy!

One way to do that is to create a function that takes a date as a parameter, subtracts one day, and returns it.

Following what we did in the last part, here’s how:

// Create a function to make the logic generic
const removeOneDayToDate = (date) => {
  date.setDate(date.getDate() - 1)

  return date
}

// Get the current date
const date = new Date()

// Before removing 1 day
console.log(date.toString())
// Output: "Tue Nov 15 2022 13:37:12 GMT+0100 (Central European Standard Time)"

// Call `removeOneDayToDate` with the current date
// and assign the result to a new variable called `yesterdayDate`
const yesterdayDate = removeOneDayToDate(date);

// After subtracting 1 day
console.log(yesterdayDate.toString());
// Output: "Mon Nov 14 2022 13:37:12 GMT+0100 (Central European Standard Time)"
Enter fullscreen mode Exit fullscreen mode

Thanks for reading. Let’s connect!

➡️ I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more. 🚀🎒

Top comments (2)

Collapse
 
hamzamateen profile image
HamzaMateen

nice and simple

Collapse
 
gaelgthomas profile image
Gaël Thomas

Thanks, @hamzamateen!