gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:39:38 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com