javascript - Dayjs:无法将自定义 24 小时时间格式化为 12 小时

标签 javascript time-format dayjs

import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);

当执行类似这样的 "dayjs().format('18:00', "hh:mm A");" 时,它不会转换为 12 小时计时.... 18:00返回

我如何使用 dayjs 转换输出,例如:

 08:00 -> 08:00 AM
 18:00 -> 06:00 PM

最佳答案

我接受了已接受的答案,并针对我的用例对其进行了一些简化。这几乎是相同的用例。

如果日期无关紧要,而您只是想转换时间 - 那么这应该就是您所需要的。

const globalTime = "14:00";
const twelveHourTime = dayjs('1/1/1 ' + globalTime).format('hh:mm a')
// twelveHourTime = "02:00 pm"

由于您需要 dayjs 的完整日期来格式化它,但唯一的输出是时间,那么日期是无关紧要的,可以是任何东西。比如,“1/1/1”

关于javascript - Dayjs:无法将自定义 24 小时时间格式化为 12 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63602831/

相关文章:

javascript - 在 jQuery 中获取变量

javascript - 如何从 Servlet 中的 <div contenteditable ="true"> 中检索值

javascript - 对象数组的 Angular JSON 过滤器

locale - dayjs 无法正确解析英式英语日期

javascript - 如何在不删除选择框滚动条的情况下删除浏览器滚动条

php - 以半小时为增量循环遍历时间范围

javascript - 整数和小数小时 Double 值转换为 12 小时制时钟格式时间

java - Android 将日期格式从字符串更改为日期和时间分隔

javascript - 简化 JavaScript