get previous day using JavaScript

To get previous date. You only need to create a new Date object then get date from newly created object using getDate() and minus one day as getDate()- 1 and pass it to previous day new Date object.

The getDate() method returns the day of current month according to user browser timeZone

The setDate() method sets the day of the month by getting a number between the days of month

 

We create a new variable (previousDay) because the setDate method mutates the Date object it.

let currentDate = new Date();
let previousDay = new Date(currentDate.getTime());
previousDay.setDate(currentDate.getDate() - 1);
previousDay;

 

Simplest example to get pervious day is as follows 

let date = new Date();
date.setDate(date.getDate() - 1);

console.log(date)

Tags:

Share:

Related posts