Get current date using new Date()
and then get current time from current date object using Date.prototype.getTime()
. and pass it to new date object as previous day then use the set date method to get previuos day
The getTime()
method returns the number of milliseconds since the epoch, which is defined at January 1, 1970,
Use the Date.prototype.setTime()
to get previous date. You only need to create a new Date
object then get date from newly created object and minus one day as written in bellow and pass it to previous day new Date
object.
The setTime()
method sets the new Date
object to the time by getting thee EPOCH time.
The getTime()
method returns the number of milliseconds since the EPOCH, which is defined at January 1, 1970,
The setDate()
method sets the day of the month by getting a number between the days of month
let currentDate = new Date();
let previousDay = new Date(currentDate.getTime());
previousDay.setDate(currentDate.getDate() - 1);
previousDay;