Get the Previous day of a Date (or yesterday) in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 5, 2024
4 min

banner

# Table of Contents

  1. Get the Previous day of a Date (or yesterday) in JavaScript
  2. Get yesterday's Date formatted as YYYY-MM-DD in JavaScript

# Get the Previous day of a Date (or yesterday) in JavaScript

Use the setDate() method to get the previous day of a date, e.g. date.setDate(date.getDate() - 1).

The setDate method takes the day of the month as a parameter and changes the value for the Date instance.

index.js
function getPreviousDay(date = new Date()) { const previous = new Date(date.getTime()); previous.setDate(date.getDate() - 1); return previous; } console.log(getPreviousDay()); // ๐Ÿ‘‰๏ธ yesterday // ๐Ÿ‘‡๏ธ Fri Dec 23 2022 console.log(getPreviousDay(new Date('2022-12-24'))); // ๐Ÿ‘‡๏ธ Sat Dec 31 2022 console.log(getPreviousDay(new Date('2023-01-01')));

get previous day of date or yesterday

The code for this article is available on GitHub

We created a reusable function that returns the previous day of a date.

We defined the previous variable because the Date.setDate() method mutates the Date object it was called on.

This might not be what you want if you need to preserve the passed in Date object.

On the first line of a function, we created a new Date object that stores the same date and time.

The setDate method takes an integer between 1 and 31 that represents the day of the month and changes the value on the given Date object.

We used the getDate() method to get the day of the month of the specific date and then decremented the result by 1.

The getDate method returns an integer between 1 and 31.

We used a default parameter when defining the getPreviousDay function. If the user doesn't pass in a Date object, the function returns yesterday's date.

Note that the Date object automatically handles the scenario where the date is the first of the month.

If that is the case, the Date object rolls back to the previous month automatically.

index.js
function getPreviousDay(date = new Date()) { const previous = new Date(date.getTime()); previous.setDate(date.getDate() - 1); return previous; } // ๐Ÿ‘‡๏ธ Tue Jan 31 2023 console.log(getPreviousDay(new Date('2023-02-01'))); // ๐Ÿ‘‡๏ธ Sat Dec 31 2022 console.log(getPreviousDay(new Date('2023-01-01')));

The Date object also handles the scenario where the previous day is the last day of the previous year.

# Get yesterday's Date formatted as YYYY-MM-DD in JavaScript

To get yesterday's date formatted as YYYY-MM-DD:

  1. Decrement 1 day from the current date to get yesterday's date.
  2. Get the year, month and day of the month values from the date.
  3. Join the values with a hyphen separator.
index.js
const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); console.log(yesterday); // ๐Ÿ‘‰๏ธ "Mon Jul 24 2023" function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } console.log(formatDate(yesterday)); // ๐Ÿ‘‰๏ธ "2023-07-24"

get yesterday date formatted as yyyy mm dd

The code for this article is available on GitHub

The Date() constructor returns the current date and time.

We used the Date.setDate() method to change the day of the month on the Date object.

The setDate method takes an integer 1-31 that represents the day of the month and changes the value on the given Date object.

We used the getDate method to get the day of the month for the specific date and then decremented the result by 1.

The getDate method returns an integer between 1 and 31.

Note that the Date object automatically handles the scenario where the date is the first of the month.

If that is the case, the Date object rolls back to the previous month automatically.

We defined a padTo2Digits function that helps us handle the case where the month or day of the month is less than 10.

To get consistent results, we pad the values of the month and date with a leading 0 if they are less than 10.

The formatDate function takes a Date object as a parameter and returns a string formatted as YYYY-MM-DD.

The function makes use of the following 3 Date related methods.

  • Date.getFullYear() method - returns a four-digit number representing the year that corresponds to a date.

  • Date.getMonth() - returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1.

  • Date.getDate() - returns an integer between 1 and 31 representing the day of the month for a specific date.

The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11.

Because the getMonth method is zero-based we added 1 to its return value.

The last step is to place the calls to the methods in an array, so we can join them by a hyphen separator. Here are some examples.

index.js
console.log(['2023', '05', '24'].join('-')); // ๐Ÿ‘‰๏ธ '2023-05-24' console.log(['2023', '09', '13'].join('-')); // ๐Ÿ‘‰๏ธ '2023-09-13'

You could easily reorder the values in the array, change the separator to a forward slash /, or whatever suits your use case.

This formats yesterday's date as YYYY-MM-DD. Here is the complete code snippet.

index.js
const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); console.log(yesterday); // ๐Ÿ‘‰๏ธ Thu Jan 13 2022 function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function formatDate(date) { return [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-'); } console.log(formatDate(yesterday)); // ๐Ÿ‘‰๏ธ "2022-01-13"
The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev