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

222
Views
Formatting 12 hour time (h:mm A) to 24 hour time (HH:mm:00) in day.js

I am trying to make a time conversion function that takes time in as "h:mm A" and converts it to military time (HH:mm:00) in day.js, but am struggling to figure it out. I was able to complete this task without dayjs but can't quite figure it out with dayjs. Here is my attempt at it:

The 00 is there as I want the seconds to default to 00. Thank you!

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

Just prepend any date to make a valid dayjs date object

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

There are a couple of errors in your code:

  • the function name is dayjs, not day
  • this formatting requires a plugin (customParseFormat - "String + Format" depends on this plugin; it's noted on the top part of the docs (in yellow)); you have to load this plugin for the syntax to work
  • after loading the plugin, you have to extend dayjs with the new capabilities

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

EDIT: updated code

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