Javascript – get yesterday’s date

clock in front of a code

Kompot

1. method: setDate

First, let’s initiate the date and assign it to the “yesterday” variable

const yesterday = new Date()

Next, we will use setDate to set it to yesterday’s date.

const yesterday = new Date()

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

And now, yesterday’s variable has yesterday’s date.
Easy as that!

2. method: Quick and Easy oneliner

The first and only step is to initialize the Date with today’s date and subtract day’s worth of milliseconds, aka 86400000

const yesterday = new Date(Date.now() - 86400000)

And there we go! Yesterdays date

Leave a Comment

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