简体   繁体   中英

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

I need to convert backend-fetched data "17:00" to "5pm". Using moment I just do:

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

How to achieve the same thing with Luxon ? While moment allows to format times directly, it seems Luxon 'sformat functions require a date (which I don't have).

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
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`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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