简体   繁体   English

如何使用 Luxon 格式化时间(不是日期)?

[英]How to format a time (not a date) using Luxon?

I need to convert backend-fetched data "17:00" to "5pm".我需要将后端获取的数据“17:00”转换为“5pm”。 Using moment I just do:使用我只是做的moment

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

How to achieve the same thing with Luxon ?如何与Luxon实现相同的Luxon While moment allows to format times directly, it seems Luxon 'sformat functions require a date (which I don't have).虽然moment允许直接格式化时间,但Luxon格式化函数似乎需要一个日期(我没有)。

Any suggestion?有什么建议吗?

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

couldn't figure out how to make meridiem lowercase though虽然不知道如何使 meridiem 小写
https://github.com/moment/luxon/issues/224 https://github.com/moment/luxon/issues/224

EDIT if want in lowercase can do something like this编辑如果想要小写可以做这样的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM