Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You forgot to remove the time from date3m before setting the input's max attribute to it.

let today = new Date().toISOString().split('T')[0];
let date3m = new Date(new Date().setDate(new Date().getMonth() + 3)).toISOString().split('T')[0];

document.getElementsByName("start")[0].setAttribute('min', today);
document.getElementsByName("start")[0].setAttribute('max', date3m)
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>
<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>

about 4 years ago · Juan Pablo Isaza Relatório

0

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>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda