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

avatar

Borislav Hadzhiev

Last updated: Jan 14, 2022

banner

Photo from Unsplash

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); // 👉️ "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 Date() constructor returns the current date and time.

We used the 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 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 values are 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"
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.