Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!