If you’re working with dates in JavaScript, you may sometimes need to get yesterday’s date. Whether you’re logging events, showing date filters, or simply manipulating dates, it’s a common task.
In this article, you’ll learn how to get yesterday date in JavaScript using simple and clear examples. We’ll use the built-in Date
object and explore the best way to subtract one day from a date in JavaScript.
Let’s dive in.
To get yesterday’s date, we can simply create a Date
object for today, and then subtract one day from it.
// Create a new Date object for today
const today = new Date();
// Copy the current date so we don't change the original
const yesterday = new Date(today);
// Subtract one day (24 hours) from the current date
yesterday.setDate(today.getDate() - 1);
// Show the result
console.log(yesterday);
What’s happening here?
new Date()
creates an object representing the current date and time.- We make a copy of the date to avoid mutating the original.
setDate()
is used to subtract one day usinggetDate() - 1
.
The result will be a Date
object that represents yesterday’s date.
You can also format the date to a more readable string, like this:
const formatted = yesterday.toISOString().split('T')[0];
console.log(formatted); // Output: something like "2025-05-01"
Explanation:
toISOString()
gives the full ISO string:"2025-05-01T14:34:00.000Z"
- We split it at the
"T"
and take the first part to get the date only.
This is useful if you want to display or save the date as a string.
Sometimes you may want to calculate the date in UTC time (not local time).
Here’s how:
const now = new Date();
// Get UTC date and subtract one
const utcYesterday = new Date(Date.UTC(
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate() - 1
));
console.log(utcYesterday.toUTCString());
Explanation:
Date.UTC()
creates a date in UTC.- We subtract one from
getUTCDate()
to get yesterday in UTC. toUTCString()
prints it in a readable UTC format.
You might want to create a reusable function for your app:
function getYesterdayDate() {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
return yesterday;
}
console.log(getYesterdayDate());
This simple function gives you a clean way to reuse the logic whenever needed.
Need to get the day before a custom date?
const input = new Date('2025-01-10');
input.setDate(input.getDate() - 1);
console.log(input); // Output: 2025-01-09
This works exactly the same for any custom date. Just subtract one using setDate()
.
Getting yesterday’s date in JavaScript is simple and requires just a few lines of code. No need for complex libraries. All you need is the built-in Date
object and a call to setDate()
.
If you’re interested in exploring more, check out these articles.
- 4 Ways To Handle Asynchronous JavaScript
- Overloading vs Overriding in TypeScript
- How to Use as const in TypeScript Effectively
Want more dev insights like this? Subscribe to get practical tips, tutorials, and tech deep dives delivered to your inbox. No spam, unsubscribe anytime.