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

193
Visualizações
Get the percentage of the day that has elapsed

I need to get the percentage of the day that has elapsed in 24 hour time. 24:00 being 100%, 12:00 being 50%, and 00:00 being 0%. Here is what I have, however, the percentage is wrong:

function currentTime() {
  let date = new Date(); 
  let hh = date.getHours();
  let mm = date.getMinutes();
  let ss = date.getSeconds();
  let ms = date.getMilliseconds();
  let session = "AM";

  if(hh === 0){
      hh = 12;
  }
  if(hh > 12){
      hh = hh - 12;
      session = "PM";
  }

  hh = (hh < 10) ? "0" + hh : hh;
  mm = (mm < 10) ? "0" + mm : mm;
  ss = (ss < 10) ? "0" + ss : ss;

  let time = hh + ":" + mm + " " + session;

  document.getElementById("clock").innerText = time; 
  let t = setTimeout(function(){ currentTime() }, 1000);

  percentage = (hh / 36 + mm / (60 * 24)) * 1000;

  document.getElementById("percentage").innerText = percentage; 
}

currentTime();
percentage();
<div>
    <span id="percentage" onload="percentage()"></span>% elapsed
</div>

I would like to understand what I'm doing wrong and how this can be corrected. Thanks.

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

0

So, I'd reccomend forgoing the whole logic of hours and minutes and stuff.

You could simply say:

function getDatePercent() {
  let dateInQuestion = new Date(Date.now())

    //we are copying the value of the date object into a new object:
    let startOfDay = new Date(dateInQuestion.valueOf())
    
    //define the beginning of the day. Depending on time zone and browser, this may need tweaking:
    startOfDay.setHours(0)
    startOfDay.setMinutes(0)
    startOfDay.setSeconds(0)
    startOfDay.setMilliseconds(0)

    let lengthOfDay = 1000 * 60 * 60 * 24 //ms in a day

    //subtract to find time since beginning of the day, divide by
    //number of ms in day, and then multiply by 100 to get percentage
    return ( dateInQuestion.valueOf() - startOfDay.valueOf() ) / lengthOfDay * 100
}

console.log(getDatePercent())

This allows us to more directly measure how many ms have eslapsed since the start of the day compared to how many ms there are in the day.

Note: this snippet was running based on UTC time, not local time. That's why I didn't originally make it a snippet. Someone edited my answer.

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