最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Formatting 12 hour time (h:mm A) to 24 hour time (HH:mm:00) in day.js - Stack Overflow

programmeradmin0浏览0评论

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 plete 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="/[email protected]/dayjs.min.js"></script>

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 plete 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/npm/[email protected]/dayjs.min.js"></script>

Share Improve this question edited Jun 30, 2022 at 19:34 mplungjan 178k28 gold badges181 silver badges240 bronze badges asked Jun 30, 2022 at 19:14 karjeezykarjeezy 611 gold badge1 silver badge9 bronze badges 6
  • I made you a snippet - I removed the typescript to make it a runnable snippet. You had a typo with the quotes, but your code stull does not work. What is a valid date input and what is the function day you are calling? Please fix the script to a minimal reproducible example – mplungjan Commented Jun 30, 2022 at 19:25
  • The A that I am using is the dayjs formatting for AM/PM @mplungjan – karjeezy Commented Jun 30, 2022 at 19:31
  • A valid date input would be say "10:24 AM" or "8:55 PM" – karjeezy Commented Jun 30, 2022 at 19:32
  • So what is day() - I updated your snippet with 10:24 AM – mplungjan Commented Jun 30, 2022 at 19:34
  • @mplungjan I'm asking the question because of the fact that I couldn't get it to work. Day() was just modeled after the approach that would be applicable to momentjs. Not too sure how to make this code work though for intended purposes. – karjeezy Commented Jun 30, 2022 at 19:49
 |  Show 1 more ment

2 Answers 2

Reset to default 4

Just prepend any date to make a valid dayjs date object

Note: this is the lazy way of allowing a time string. To adhere to the documentation of dayJS. look at the other answer

Or don't use dayJS at all

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/npm/[email protected]/dayjs.min.js"></script>

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

(Edited the code: removed the double call of dayjs in the conditional.)

// extend dayjs with the loaded customParseFormat plugin
dayjs.extend(window.dayjs_plugin_customParseFormat)

function convertToMilitaryTime(formattedTime) {
  if (formattedTime) { //formattedTime is '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/npm/[email protected]/dayjs.min.js"></script>
<!-- load the required plugin: -->
<script src="https://unpkg./[email protected]/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/npm/[email protected]/dayjs.min.js"></script>
<!-- load the required plugin: -->
<script src="https://unpkg./[email protected]/plugin/customParseFormat.js"></script>

发布评论

评论列表(0)

  1. 暂无评论