[removed] convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

前端 未结 16 1401
野性不改
野性不改 2020-11-30 03:35

The server is sending a string in this format: 18:00:00. This is a time-of-day value independent of any date. How to convert it to 6:00PM in Javasc

16条回答
  •  难免孤独
    2020-11-30 04:16

    This might help to format if you are using ES6.
    Below code snippet will ignore the seconds. If you want to consider seconds you can add that as the first parameter.

       const formatFrom24Hrsto12Hrs = (time, ignoreZero = true) => {
          let [hours, minutes] = time.split(':')
          let modifier = +hours < 12 ? 'am' : 'pm'
          hours = +hours % 12 || 12
          minutes = ignoreZero && +minutes === 0 ? '' : `:${minutes}`
          return hours + minutes + modifier
        }
    

提交回复
热议问题