0

I'm looking for the simplest way to add dynamic dates to my events.

(function() {
  document.querySelector("#startDate").valueAsDate = new Date();
  document.querySelector("#endDate").valueAsDate = new Date();
  document.querySelector("#endDate").min = new Date()
    .toISOString()
    .substr(0, 10);

I was expecting, to be able to add dates simply, but i cant think of a way to do it.

  • For adding days to a Date, check out: https://stackoverflow.com/questions/563406/how-to-add-days-to-date?rq=1 – Stephen Quan Dec 06 '22 at 22:14
  • For converting dates to yyyy-mm-dd format check out: https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd – Stephen Quan Dec 06 '22 at 22:41
  • your way of initializing an input date is wrong, because it does not take into account the time difference, which can distort the date's value. see https://stackoverflow.com/questions/28729634/set-values-in-input-type-date-and-time-in-javascript/58252034#58252034 – Mister Jojo Dec 06 '22 at 22:46

1 Answers1

0

You can add days to any Date object by using .getDate() and .setDate():

const today=new Date(), in10days=new Date();
in10days.setDate(in10days.getDate()+10);

console.log("today:",today.toISOString())
console.log("in 10 days:",in10days.toISOString());
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43