Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda