I am writing a tampermonkey script and the logic is as follows.
- make a GET request to fetch a certain value, and this will give me a date in the format '2021-05-01' This is date 1st May 2021.
- Now I get to the following dates within my javascript. 2 days before and after this date.
- so I want to calculate the following dates 29th April 2021, 30th April 2021, 2nd May 2021, 3rd May 2021. i.e. '2021-04-29','2021-04-30','2021-05-02','2021-05-03'
Please can someone help me write this.
My current code is as follows
//GET request to get a date value and save it to a variable called checkdate
var checkdate = GET-request.
// add code to get 2 days plus and minus this date.
var d = new Date(checkdate)
var x = 2
var newd = d.setDate(d.getDate() - x);
var dt = getFullYear() + "-" + (dt.getMonth() + 1) + "-" + dt.getDate();
So when I run it for the date '2021-05-01', I end up with the following value for newd 1619654400000 and value for dt as 2021-04-29
so now I need to extract the date from this which should be '2021-04-29' and now I have to repeat this same code 4 times. Is there a better way to do this.