Skip to content

How to get yesterday's date using JavaScript

New Course Coming Soon:

Get Really Good at Git

How do you get yesterdays' date using JavaScript?

Well, first you get the date at the current time (today), then you subtract a day from it:

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)

today.toDateString()
yesterday.toDateString()

We use the setDate() method on yesterday, passing as parameter the current day minus one.

Even if it’s day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code

Here is how can I help you: