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

169
Visualizações
How to check that the user has selected 3 years before date in the textbox in JavaScript

I am new to coding I have confused about the concept of Dates in JavaScript. I need to validate that the user has selected 3 years before's date in the textbox*, but my codes through some errors.* Check The Following Code:

<script>
      function run() {
        var dt =document.getElementById("dt").value; //Value of selected date
        var ndt = new Date(); //Current Date
        var diff = ndt.getTime()-dt.getTime();
        var milliseconds =  1000 * 60 * 60 * 24;
        var result = diff/milliseconds;

        if (result > 1095) {
          alert("Correct");
        } else {
          alert("Wrong")
        };
      }
      </script>

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

0

Your calculations should be something along the lines:

if (diff > 1000 * 60 * 60 * 24 * 365 * 3)

This is because: 1000ms (is 1 second) * 60 (equals 1 minute) * 60 (equals 1 hour) * 24 (equals one day) * 365 (equals 1 year, roughly) * 3 (equals 3 years).

Surely that's pretty prone to errors! Leap-years are not taken to account, and many more.

I'd suggest you also check this one if you decide to go on a more-stable path: How can I calculate the number of years between two dates?

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