【问题标题】:How to format a time (not a date) using Luxon?如何使用 Luxon 格式化时间(不是日期)?
【发布时间】:2021-12-12 17:22:18
【问题描述】:

我需要将后端获取的数据“17:00”转换为“5pm”。使用moment 我只是这样做:

moment('17:00', ['HH']).format('h:mma')

如何使用Luxon 实现相同的功能?虽然moment 允许直接格式化时间,但似乎Luxonformat functions 需要一个日期(我没有)。

有什么建议吗?

【问题讨论】:

  • 为什么不使用今天的日期和 BE 指定的小时数?

标签: javascript date time momentjs luxon


【解决方案1】:
DateTime.fromISO('17:00').toLocaleString(DateTime.TIME_SIMPLE)    // 5:00 PM
DateTime.fromISO('17:00').toFormat('h:mma')                       // 5:00PM
DateTime.fromISO('17:00').toFormat('h:mm a')                      // 5:00 PM

无法弄清楚如何将 meridiem 设为小写
https://github.com/moment/luxon/issues/224

EDIT如果想要小写可以做这样的事情

  const dt = DateTime.fromISO('17:00')
  const meridiem = (dt.hour > 11) ? 'p' : 'a'
  const final =  `${dt.toFormat('h:mm')}${meridiem}m`

【讨论】:

    猜你喜欢
    • 2021-01-11
    • 2021-08-30
    • 2021-07-28
    • 2022-08-19
    • 2021-02-14
    • 2022-10-13
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多