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

188
Visualizações
Obtener el porcentaje del día que ha transcurrido

Necesito obtener el porcentaje del día que ha transcurrido en 24 horas. 24:00 siendo 100%, 12:00 siendo 50% y 00:00 siendo 0%. Esto es lo que tengo, sin embargo, el porcentaje es incorrecto:

 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>

Me gustaría entender qué estoy haciendo mal y cómo se puede corregir. Gracias.

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

0

Entonces, recomendaría renunciar a toda la lógica de horas y minutos y esas cosas.

Simplemente podrías decir:

 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())

Esto nos permite medir más directamente cuántos ms han transcurrido desde el comienzo del día en comparación con cuántos ms hay en el día.

Nota: este fragmento se estaba ejecutando en función de la hora UTC, no de la hora local. Es por eso que originalmente no lo convertí en un fragmento. Alguien editó mi respuesta.

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