【问题标题】: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 允许直接格式化时间,但似乎Luxon 的format functions 需要一个日期(我没有)。
有什么建议吗?
【问题讨论】:
标签:
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`