dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

en-gb ordinal formatted number has surrounding square brackets

Open jwarby opened this issue 2 years ago • 1 comments

Describe the bug I'm not sure if this is intentional or not, but when using dayjs.localeData().ordinal(2) with en-gb locale, the result string is wrapped in square brackets, eg "[2nd]".

Expected behavior The ordinal number to be returned sans square brackets, eg "2nd"

Information

  • Day.js Version: v1.11.2
  • OS: Ubuntu 20.04
  • Browser: Chrome 99
  • Time zone: GMT+01:00 BST (British Summer Time)

jwarby avatar May 11 '22 09:05 jwarby

At the moment the dayjs.localeData().ordinal(..) value is only used by the advancedFormat plugin and here the square brackets are interpreted as 'escape characters' and are automatically removed.

~~But to be consistent with all other locale plugins, the square brackets should be removed. I will create a pr for this.~~

The square brackets ('the escaping') are necessary, because e.g. 2nd contains the 'd' character, which is a placeholder for the day of the week and without the square brackets '2nd' would result in a formatted date containing the '2n' plus the number of the day of the week (e.g. '2n4').

BePo65 avatar Jun 07 '22 04:06 BePo65

Thanks for looking into this @BePo65 and sorry for my late response back, I didn't seem to get a notification from GitHub about this.

What you've said makes sense, and I've actually hit the issue you were describing with the workaround I had applied (I overrode the locale data to remove the square brackets). I think this just stems from me trying to (ab)use Day.js to format a plain number as an ordinal, so I've changed my approach to use react-intl's selectordinal formatting instead for this particular use case (just mentioning that here in case anybody else comes across this issue and is trying to do the same thing as me).

Thanks again!

jwarby avatar Oct 20 '22 08:10 jwarby