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

246
Views
Can't convert string time in moment to correct format

Hey I have a react app where I made a slider for time. the time is for example: 3 PM or 7:30 AM like this format.

But I can't seem to format it correctly using moment, it is always returning AM Here is my implementation:

moment(dropoffTime.formattedTime, ['h:mm A'])
          .locale('en')
          .format('hh:mm A')
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

.format working fine and giving PM for me right now. Maybe you can console.log dropoffTime.formattedTime in order to see what is the value before formatting

console.log(moment(new Date())
          .locale('en')
          .format('hh:mm A'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

moment accepts dates either as a Date object or in a string format:

const dropoffTime = new Date();

const str = moment(dropoffTime)
          .locale('en')
          .format('hh:mm A');
          
console.log(str)
document.querySelector("input").addEventListener("input",function(){
  console.log(moment(this.value, ['h:mm A']).locale('en').format('hh:mm A'))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<input type="time" value="12:34" >

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!