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

223
Views
Formateo de 12 horas (h:mm A) a 24 horas (HH:mm:00) en day.js

Estoy tratando de hacer una función de conversión de tiempo que tome el tiempo como "h: mm A" y lo convierta a tiempo militar (HH: mm: 00) en day.js, pero estoy luchando por resolverlo. Pude completar esta tarea sin dayjs pero no puedo resolverlo con dayjs. Aquí está mi intento de hacerlo:

El 00 está ahí porque quiero que los segundos sean 00 por defecto. ¡Gracias!

 function convertToMilitaryTime(formattedTime) { if (formattedTime) { //formattedTime is 'h:mm A' const formatted = dayjs(formattedTime, "h:mm A") return day(formattedTime, ['h:mm A']).format("HH:mm:00") } return formattedTime; } console.log(convertToMilitaryTime("10:24 AM")); // not a valid date string
 <script src="https://cdn.jsdelivr.net/npm/dayjs@1.11.3/dayjs.min.js"></script>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplemente anteponga cualquier fecha para hacer un objeto de fecha dayjs válido

 const ampm2military = ampm => ampm ? dayjs(`1/1/1 ${ampm}`).format("HH:mm:00") : null; console.log(ampm2military("1:24 PM"));
 <script src="https://cdn.jsdelivr.net/npm/dayjs@1.11.3/dayjs.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

Hay un par de errores en tu código:

  • el nombre de la función es dayjs , no day
  • este formato requiere un complemento ( customParseFormat - "String + Format" depende de este complemento; se indica en la parte superior de los documentos (en amarillo)); tienes que cargar este complemento para que la sintaxis funcione
  • después de cargar el complemento, debe extender dayjs con las nuevas capacidades

 // extend dayjs with the loaded customParseFormat plugin dayjs.extend(window.dayjs_plugin_customParseFormat) function convertToMilitaryTime(formattedTime) { if (formattedTime) { //formattedTime is 'h:mm A' const formatted = dayjs(formattedTime, "h:mm A") // the function name is dayjs, not day return dayjs(formattedTime, ['h:mm A']).format("HH:mm:00") } return formattedTime; } console.log(convertToMilitaryTime("10:24 AM")); // not a valid date string
 <script src="https://cdn.jsdelivr.net/npm/dayjs@1.11.3/dayjs.min.js"></script> <!-- load the required plugin: --> <script src="https://unpkg.com/dayjs@1.11.3/plugin/customParseFormat.js"></script>

EDITAR: código actualizado

 // extend dayjs with the loaded customParseFormat plugin dayjs.extend(window.dayjs_plugin_customParseFormat) const convertToMilitaryTime = (ft) => dayjs(ft, "h:mm A", "en", true).isValid() ? dayjs(ft, 'h:mm A').format("HH:mm:00") : ft console.log(convertToMilitaryTime("10:24 AM")); // not a valid date string
 <script src="https://cdn.jsdelivr.net/npm/dayjs@1.11.3/dayjs.min.js"></script> <!-- load the required plugin: --> <script src="https://unpkg.com/dayjs@1.11.3/plugin/customParseFormat.js"></script>

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!