0

I got the following variables:

today = new Date();
date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();

console.log(date)

------
Output
------
2021-9-6

How can I get the date from yesterday or 2 day ago? I tried:

console.log(today-2)
console.log(date-2)
------
Output
------
1630936602270
NaN

I expected the follwing output 2021-9-4

Paul Viorel
  • 234
  • 1
  • 11

1 Answers1

1
let twoDaysAgoInMs =2*24*3600*1000;
console.log(new Date((new Date()).getTime()- twoDaysAgoInMs))


 // to get only YYYY-mm-dd
 console.log(new Date((new Date()).getTime()- twoDaysAgoInMs).toISOString().substring(0,10))
koalaok
  • 5,075
  • 11
  • 47
  • 91