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

186
Visualizações
Using two gethours() but always same result in javascript

I wanted to get start time and end time separately so I wrote code like this:

const date = new Date();
let start = date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 60 / 60;

function startRecord() {
  document.getElementById("1").innerHTML = start;
}

function endRecord() {
  let end = date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 60 / 60;
  document.getElementById("2").innerHTML = end;
}
<p id="1">Show start time</p>
<p id="2">Show end time</p>

<button type="button" onclick="startRecord()">
  start
</button>
<button type="button" onclick="endRecord()">
  end
</button>

But I always got same result.
Start time and end time is same although I spent some time between them.
How can I get correct result?

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

0

Because you have only one instance of date. You must use 2 variables of date.

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to make a new date object if you want a new time. Change your javascript to this:

    let date = new Date();
    let start = date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 60 / 60;
    function startRecord() {
      date = new Date();
      document.getElementById("1").innerHTML = start;
    }
    function endRecord() {
      date = new Date();
      let end = date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 60 / 60;
      document.getElementById("2").innerHTML = end;
    }
about 4 years ago · Juan Pablo Isaza Relatório

0

Your code gets the current date/time at the time the script loads, not when the button is clicked.

You need to get the date/time in the click handler, so it reflects the moment the click happened.

Since you need to do the same calculation in both click handlers, put that logic in a function and call it from both click handlers:

function getCurrentTime() {
  const date = new Date();
  return date.getHours() + date.getMinutes() / 60 + date.getSeconds() / 60 / 60;
}  

function startRecord() {
  document.getElementById("1").innerHTML = getCurrentTime();
}

function endRecord() {
  document.getElementById("2").innerHTML = getCurrentTime();
}
<p id="1">Show start time</p>
<p id="2">Show end time</p>

<button type="button" onclick="startRecord()">
  start
</button>
<button type="button" onclick="endRecord()">
  end
</button>

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