How to Get Yesterday's Date using Javascript

By Henri Parviainen

Yesterday date in javascript

This tutorial will introduce you to two ways to get the date value for yesterday using Javascript. You can then use the method that you prefer.

Method 1 - using Date.SetDate()

This method is quite simple. First, we get a date for today by calling new Date() which creates a new javascript date object with the current date.

Then we can get a date for yesterday by subtracting one day from today by using the setDate() method.

const today = new Date();

console.log(today.toDateString());

// Sat Mar 19 2022

const yesterday = new Date(today.setDate(today.getDate() - 1));

console.log(yesterday.toDateString());

// Fri Mar 18 2022

Method 2 - Using milliseconds

The second way to get a date for yesterday is to use milliseconds. As we know that one day is 24 hours which is 86400000 milliseconds, we can subtract that from any date, and we will always get the date for yesterday.

const today = new Date();

console.log(today.toDateString());

// Sat Mar 19 2022

const yesterday = new Date(today - 86400000);

console.log(yesterday.toDateString());

// Fri Mar 18 2022

SHARE