How To Get Yesterday’s Date Formatted As YYYY-MM-DD In JavaScript

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

This tutorial will show you how to get yesterday’s Date formatted as YYYY-MM-DD in JavaScript. You can use some JavaScript Date methods to get and format the date as you want.

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

You can get a yesterday Date object in JavaScript by creating new Date objects and using the getDate() and setDate() methods.

Example code:

let todayObj = new Date();
let yesterdayObj = new Date();
yesterdayObj.setDate(todayObj.getDate() - 1);

console.log("Today:", todayObj);
console.log("Yesterday:", yesterdayObj);

Output:

Today: 2022-09-14T10:17:08.502Z
Yesterday: 2022-09-13T10:17:08.502Z

The output may differ slightly depending on the environment you run your code.

In the following sections, I will show you how to format the Date object to YYYY-MM-DD.

Get yesterday’s date object and format it with the toISOString() method

After having yesterday’s date object, you can use the toISOString() method to convert it to a string. The toISOString() method returns a string with the date and time in the format “YYYY-MM-DDTHH:mm:ss.sssZ”, so you can get your date in YYYY-MM-DD by removing the time part. Here, I use the substring() method to get the first ten characters of the date string.

Example code:

let todayObj = new Date();
let yesterdayObj = new Date();
yesterdayObj.setDate(todayObj.getDate() - 1);

let yesterday = yesterdayObj.toISOString().substring(0, 10);
console.log("Yesterday:", yesterday);

Output:

Yesterday: 2022-09-13

Format string from yesterday’s date, month, and year values

A more flexible way is to separate yesterday’s date, month, and year values. Then you can format your date string in many ways, more than the format YYYY-MM-DD.

To get the date, month, and year of a Date object, you need to use the getDate(), getMonth(), and getFullYear() methods. There are a few things you need to notice when using these methods.

  • First, the getMonth() method return the month in 0 index. So the value of September is 8. Therefore, you need to add 1 to the value to get the correct month value.
  • Second, with the values of date and month being single-digit numbers, you need to prepend them by 0 to get the correct format YYYY-MM-DD. See how I do this in the example below.

Example code:

let todayObj = new Date();
let yesterdayObj = new Date();

yesterdayObj.setDate(todayObj.getDate() - 1);

let date = ("0" + (yesterdayObj.getDate())).slice(-2);
let month = ("0" + (yesterdayObj.getMonth() + 1)).slice(-2);
let year = yesterdayObj.getFullYear();

let yesterday = year + '-' + month + '-' + date;
console.log("Yesterday:", yesterday);

Output:

Yesterday: 2022-09-13

Summary

In this tutorial, I showed you how to get yesterday’s Date formatted as YYYY-MM-DD in JavaScript. You can get yesterday’s date object and format it with the toISOString() method. A more flexible way is to separate yesterday’s date, month, and year values. Then you can format your date string in many ways, more than the format YYYY-MM-DD.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *