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

Can't set max date on date input

I am trying to set a min and max date to my date inputs, the min setting is working but for some reason the max date is not. What is the problem here?
I have also tried to use getDate()+90 instead of getMonth()+3.

my inputs:

let today = new Date().toISOString().split('T')[0];
let date3m = new Date(new Date().setDate(new Date().getMonth() + 3));
document.getElementsByName("start")[0].setAttribute('min', today);
document.getElementsByName("start")[0].setAttribute('max', date3m)
<div class="calendar">
  <div>
    <label for="start">Start Dato:</label>
    <input type="date" id="start" name="start" required>
  </div>
  <div>
    <label for="end">Slutt Dato:</label>
    <input type="date" id="end" name="end" required>
  </div>

</div>

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

>Solution :

min and max need to be in the format YYYY-MM-DD. You’re setting max to something like Sat Nov 13 2021 18:14:51 GMT-0500 (Eastern Standard Time), which doesn’t work.

Use toISOString() just like you do for today.

And setDate() should be setMonth().

let today = new Date().toISOString().split('T')[0];
let date3m = new Date();
date3m.setMonth(date3m.getMonth() + 3);
date3m = date3m.toISOString().split('T')[0];
document.getElementsByName("start")[0].setAttribute('min', today);
document.getElementsByName("start")[0].setAttribute('max', date3m)
<div class="calendar">
  <div>
    <label for="start">Start Dato:</label>
    <input type="date" id="start" name="start" required>
  </div>
  <div>
    <label for="end">Slutt Dato:</label>
    <input type="date" id="end" name="end" required>
  </div>

</div>
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