How To Check If A Date Is Yesterday Using JavaScript

Check if a Date is Yesterday using JavaScript

During JavaScript application development, you may encounter a requirement to check for a specific date, one of which is to check if a date is yesterday. This article will show you how to check if a date is yesterday using JavaScript.

Check if a date is yesterday using JavaScript

To check if a date is yesterday using JavaScript, we will take the date we need to check and compare it with yesterday. Details of this solution will be as follows:

  • First, we use the new Date() constructor to create a new date object with the current date and time.
  • Then the getDate() method is called on the created date object. The getDate() method will return the day of the month. At this point, we subtract 1 from the date we just received.
  • In the next step, we use the setDate() method with the date just received. This method will return the full day of a date.
  • Now, we have yesterday. We need to compare it with the date now to be determined. Note: Compare only the date part. Ignore the time of day part. In this step, we will use the toDateString() method. This method returns the date as a simple string and contains no hours or minutes.

Check with the current date

To check if the solution we have given is correct, first, we use yesterday as input to compare. If the result is correct, then the program has run correctly.

Code:

// Get yesterday's date of current time
const ytd1 = new Date();
ytd1.setDate(ytd1.getDate() - 1);
let text = ytd1.toDateString();
console.log(text);

// Get yesterday's date of current time
const ytd2 = new Date();
ytd2.setDate(ytd2.getDate() - 1);

// Check if ytd1 is equal to ytd2
if (ytd1.toDateString() === ytd2.toDateString()) {
	console.log("true");
} else {
	console.log("false");
}

Output:

Thu Oct 06 2022
true

With the ytd1 constant declared with yesterday’s value. The program ran correctly. Next, let’s try it with a specific date.

Check with a specific date

In the following example, we have a specific date as a string. Same as above, we also use the toDateString() method to get dates in the format without hours and minutes of yesterday and specific date to compare.

Example:

// Get date of specified time
const specificDate = new Date("2022-10-05");
let text = specificDate.toDateString();
console.log(text);

// Get yesterday's date of current time
const ytd = new Date();
ytd.setDate(ytd.getDate() - 1);

// Check if 'specificDate' is equal to 'ytd'
if (specificDate.toDateString() === ytd.toDateString()) {
	console.log("true");
} else {
	console.log("false");
}

Output:

Wed Oct 05 2022
false

To make it more convenient during application development, we should write a function that checks if the date is yesterday or not and then calls this function on the date object to be checked.

Code:

function isYtd(date) {
	// Get yesterday's date of current time
	const ytd = new Date();
	ytd.setDate(ytd.getDate() - 1);

	// Displayed on the console screen
	let text = date.toDateString();
	console.log(text);

	// Check if 'ytd' is equal to 'date'
	if (ytd.toDateString() === date.toDateString()) {
		return true;
	}
	return false;
}

const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
console.log(isYtd(yesterday));
console.log(isYtd(new Date("2022-10-05")));

Output:

Thu Oct 06 2022
true
Wed Oct 05 2022
false

Summary

This article has shown how to check if a date is yesterday using JavaScript. I hope the information in this article will be helpful to you. Thanks for reading!

Maybe you are interested:

Leave a Reply

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