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

122
Visualizações
Can any one help me on Date method of JS?

This is my HTML body section. I use onClick Method on button. Here i want the total duration of two days. My code is not working.

    <input type="date" id="d1">
    <input type="date" id="d2">
    <button onClick="calCulateDays">Get Difference</button>
    <p id="output">Hi</p>

This is Script Section

    function calCulateDays() {
        var d1 = document.getElementById("#d1").value;
        var d2 = document.getElementById("#d2").value;
        const dateOne = new Date(d1);
        const dateTwo = new Date(d2);
        const time = Math.abs(dateTwo - dateOne);
        const days = Math.ceil(time / (1000 * 60 * 60 * 24));
        document.getElementById("#output").innerHTML = days;
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

document.getElementById() expects just the ID without the '#', as @Barmer said it's weird that you called it 'calCulateDays' instead of 'calculateDays' thats why i adjusted that:

function calculateDays() {

    // Note i changed 'getElementById("#d1")' to 'getElementById("d1")'

    var d1 = document.getElementById("d1").value;
    var d2 = document.getElementById("d2").value;
    const dateOne = new Date(d1);
    const dateTwo = new Date(d2);
    const time = Math.abs(dateTwo - dateOne);
    const days = Math.ceil(time / (1000 * 60 * 60 * 24));

    // same here remove the '#'
    document.getElementById("output").innerHTML = days;

}

Here is the HTML:

<input type="date" id="d1">
<input type="date" id="d2">

// added () to the function name
<button onClick="calculateDays()">Get Difference</button>
<p id="output">Hi</p>
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