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

74
Vistas
How I make AMPM to 24 Hours in Javescript

How can I make this : "08:30AM-09:30AM" into this : "20:30-21:30" Thank you! Its Sentence (String Value)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you mean "08:30PM-09:30PM" into "20:30-21:30"

function to24Hours(time){
    let hours = time.substr(0,2);
    let minutes = time.substr(3,2);
    let AMorPm = time.substr(5,2);

    if(AMorPm ==="PM"){
        let PMhour = parseInt(hours) + 12
        let h = PMhour === 24 ? "12" : String(PMhour)
        return `${h.padStart(0,2)}:${minutes}`
    } 

    let AMhour = hours === "12" ? "00" : hours
    return `${AMhour}:${minutes}`
}

function to24HoursRange(timeRange){
    const [time1, time2] = timeRange.split("-")
    return to24Hours(time1) + "-" + to24Hours(time2)
}

console.log(to24HoursRange("08:30PM-09:30PM"))

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the moment library to solve your problem.

You can specify time to the moment constructor and then use the format option.

Example :

const time = "08:30PM-09:30PM"

const times = time.split('-')

const newTimes = times.map(t => moment(t, ["h:mm A"]).format("HH:mm"))

console.log(newTimes.join('-'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.3/moment.min.js"></script>

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