I am pulling varchar dates from a db table, and trying use them to get the date of both the previous day and next day. In the example below, I would have grabbed 2020-03-26 from my table. I am trying to figure out how to get both 2020-03-25 and 2020-03-27 saved as variables that I can then use. I have figured out that in order to get the date the exact format that I want I have to use the toISOString and slice off the first 10 characters, but I am unsure how to get the previous and next days, especially if a month crossover had occurred.
var tableDate = new Date('2020-03-26')
tableDate = tableDate.toISOString().slice(0,10)
>Solution :
Try this one:
let tableDate = new Date('2020-03-26');
// function to increment
function incrementDate(date, days) {
let result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
// funtion to decrement
function decrementDate(date, days) {
let result = new Date(date);
result.setDate(result.getDate() - days);
return result;
}
console.log(incrementDate(tableDate, 1).toISOString().split('T')[0]);
console.log(decrementDate(tableDate, 1).toISOString().split('T')[0]);