moment / luxon

⏱ A library for working with dates and times in JS
https://moment.github.io/luxon
MIT License
15.4k stars 729 forks source link

toLocaleString behaviour is not the same in different browser #1386

Closed shinnqy closed 1 year ago

shinnqy commented 1 year ago

Describe the bug DateTime.toLocaleString has different behaviour in different browser.

Edge: Version 110.0.1587.50 (Official build) (64-bit)

image

Chrome: Version 110.0.5481.78 (Official Build) (64-bit)

image

To Reproduce Please share a minimal code example that triggers the problem:

const targetStr = DateTime.fromJSDate(new Date()).setLocale('en-US').toLocaleString(DateTime.TIME_WITH_SECONDS);
const charCode = targetStr.charCodeAt(7);

Actual vs Expected behavior A clear and concise description of what you expected to happen.

Expect: the output should be same in Edge and Chrome Acutal: Edge is unicode 32, while Chrome is unicode 8239

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

diesieben07 commented 1 year ago

Luxon's localization abilities depend on the underlying browser and toLocaleString is specified to be system dependent. Most systems use Unicode ICU, but this is neither required nor specified. The output is consistent most of the time, but this can change as underlying systems update. What you are observing here is that your version of Chrome has an updated ICU and Edge does not (yet).

If you need a date / time format which must never change then toLocaleString is not the correct tool. Use toFormat for that circumstance.

You may also find the note on the return value of toLocaleString interesting:

output variations are by design and allowed by the specification. You should not compare the results of toLocaleString() to static values.

ngbrown commented 1 year ago

Using toFormat also generates different outputs:

revisionCreateDate.toFormat("fff", { locale: "en-US" })

From Node 16 (on Debian):

encodeURIComponent("August 4, 2010 at 2:51 PM UTC");
'August%204%2C%202010%20at%202%3A51%20PM%20UTC'

From Chrome 113 on Windows 10:

encodeURIComponent("August 4, 2010 at 2:51 PM UTC");
'August%204%2C%202010%20at%202%3A51%E2%80%AFPM%20UTC'