javascriptdayjs

How to format a date and write square brackets in dayjs?


In dayjs, I want to get the following date time string:

25-01-2024 [17:29]

However, when writing square brackets in the format method it's showing

25-01-2024 HH:mm

How to escape square brackets in dayjs?

import dayjs from 'dayjs'

export const formatDateTime = (dateString: string) => {
  const parsedDate = dayjs(dateString)
  const formattedDate = parsedDate.format('DD-MM-YYYY [HH:mm]')
  return formattedDate
}


Solution

  • This works:

    dayjs().format('DD-MM-YYYY [[]HH:mm]')
    

    But I hope there is a better solution