gpt4 book ai didi

javascript - 与 moment.js 相同吗?

转载 作者:行者123 更新时间:2023-11-28 15:11:15 25 4
gpt4 key购买 nike

有没有办法用 moment.js 检查两个日期是否具有相同的子午线(上午/下午)?

在文档中 isSame函数提供了测试日、月等的示例,但没有提供测试子午线的示例。

最佳答案

没有内置任何东西来进行精确比较,但您可以将两个时刻格式化为子午标记 (a) 并比较结果。

moment().format('a') === moment('2016-01-01T11:00').format('a')

这具有实际可用于任何语言环境的优点。在某些地区,它们有更多的子午线指示器,而不仅仅是 AM/PM。以阿塞拜疆语为例:

moment.locale('az')

moment('2016-01-01T02:00:00').format('hh a')
"02 gecə"

moment('2016-01-01T05:00:00').format('hh a')
"05 səhər"

moment('2016-01-01T13:00:00').format('hh a')
"01 gündüz"

moment('2016-01-01T18:00:00').format('hh a')
"06 axşam"

在源代码中,我还看到了白俄罗斯语、孟加拉语、藏语的情况,这样的例子不胜枚举。

如果您想要完整列表,请转到 Moment 源代码中的 locales 文件夹: https://github.com/moment/moment/tree/develop/src/locale

在每个语言环境中,您都会看到子午线函数。这定义了您获得该 token 的行为。

如果您不希望出现这种行为,并且希望代码始终只在“中午之前”或“中午之后”运行,您可以随时将当前的克隆快速翻转回默认区域设置:

moment('2016-01-01T18:00:00').clone().locale('en').format('a')

关于javascript - 与 moment.js 相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36365867/

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