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

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

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 Report

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 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!