Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Previous and Next Days

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading