ChakraCore icon indicating copy to clipboard operation
ChakraCore copied to clipboard

Date.prototype.toLocaleString() - "月" character is missing when "month" is specified as "short" and "year" is not specified.

Open tkiryu opened this issue 8 years ago • 8 comments

Environment

  • WIndows 10 Enterprise / 1709 / 16299.371
  • Microsoft Edge 41.16299.371.0 / Microsoft EdgeHTML 16.16299
  • IE 11.371.16299.0 / 11.0.6(KB4092946)

Symptom

Both MS Edge and IE11 have problem with Date.prototype.toLocaleString(). "月" character is missing when month is specified as short and year is not specified.

OK when neither month nor year are specified.

new Date().toLocaleString('ja-JP');
// "‎2018‎年‎4‎月‎26‎日‎ ‎11‎:‎37‎:‎33"

OK when month and year are specified.

new Date().toLocaleString('ja-JP', { day: 'numeric', month: 'short', year: 'numeric' });
// "‎2018‎年‎4‎月‎26‎日"

new Date().toLocaleString('ja-JP', { month: 'short', year: 'numeric' });
// "‎2018‎年‎4‎月"

NG when month is specified but year is not specified.

new Date().toLocaleString('ja-JP', { day: 'numeric', month: 'short' });
// "‎4‎26‎日"

new Date().toLocaleString('ja-JP', { month: 'short' });
// "‎4"

Expected result

Should be OK no matter what year is specified or not.

new Date().toLocaleString('ja-JP', { day: 'numeric', month: 'short' });
// "‎4月‎26‎日"

new Date().toLocaleString('ja-JP', { month: 'short' });
// "‎4月"

Other modern browsers

There is no problem with other modern browsers.

  • Chrome 65.0.3325.181
  • Firefox 59.0.2
  • Safari 11.1
new Date().toLocaleString('ja-JP');
// "2018/4/26 11:49:10"

new Date().toLocaleString('ja-JP', { day: 'numeric', month: 'short', year: 'numeric' });
// "2018年4月26日"

new Date().toLocaleString('ja-JP', { month: 'short', year: 'numeric' });
// "2018年4月"

new Date().toLocaleString('ja-JP', { day: 'numeric', month: 'short' });
// "4月26日"

new Date().toLocaleString('ja-JP', { month: 'short' });
// "4月"

tkiryu avatar Apr 26 '18 03:04 tkiryu

This looks like a format selection issue in Windows Globalization. @Microsoft/target-dev may be interested, but in Edge's case, there is not much we can do about this in currently-released versions of Windows.

However, you will be happy to know that the next version of Edge (releasing sometime later this year) will use ICU for Date.prototype.toLocaleString rather than Windows Globalization, and I have just confirmed that Chakra will produce your "expected" strings. Do note that your first "expected" string uses / as date separators, which seems to be a worse decision than using the actual Japanese characters that the rest of the "expected" strings use, so that may be a bug in format selection in ICU.

With that being said, we are all currently spec-compliant. The spec for toLocaleString is very implementation-defined and best-effort, and as far as the spec is concerned, new Date().toLocaleString() could produce "82" and it would still arguably be compliant. As a result, in the future, I recommend that you try using Intl.DateTimeFormat.prototype.formatToParts when available (its a brand new API) which will allow you to access specific fields of a string without needing to parse the text.

jackhorton avatar Apr 26 '18 15:04 jackhorton

@jackhorton Do you have a pointer to where Chakra is obtaining the data from Windows Globalization handy? [FYI for @Microsoft/target-dev ] This is most likely due to how Windows.Globalization handles months when you specify "abbreviated" for the month style/type versus how it handles "solo" months. It might be possible to modify Chakra to handle the case of the 'month' all by itself, but handling the 'day (numeric) + month' case would be more tricky with WinGlob, as requesting the "solo" form of the month might not always make sense. (Though this might all be moot once Chakra migrates to ICU, as you pointed out.)

jefgen avatar Apr 26 '18 19:04 jefgen

I dont understand any of the WinGlob code and its all very roundabout, but it looks like we explicitly use month.abbreviated: https://github.com/Microsoft/ChakraCore/blob/ddf2d6a8075d7c4e0630255620eb4624a5fed790/lib/Runtime/Library/InJavascript/Intl.js#L3181

jackhorton avatar Apr 27 '18 20:04 jackhorton

Thanks Jack. I was going to suggest that you might be able to 'special-case' having a single "month" element all by itself within EcmaOptionsToWindowsTemplate to get it to work... However it seems that "month.solo" isn't actually documented thought... :( So I can't really suggest doing something that isn't even documented. https://docs.microsoft.com/en-us/uwp/api/windows.globalization.datetimeformatting.monthformat

This will likely have to wait until such time that Chakra can move to using ICU.

jefgen avatar Apr 27 '18 23:04 jefgen

In keeping with the rest of the issues that will be fixed when we finish the move to ICU, I'd recommend closing this issue and tracking under #3644

dilijev avatar May 01 '18 06:05 dilijev

I vote not tracking it at all. It is fixed for all intents and purposes, its just a matter of getting all flavors of chakra/core to use ICU.

jackhorton avatar May 01 '18 16:05 jackhorton

Fair enough. Closing.

dilijev avatar May 01 '18 22:05 dilijev

I got it.

tkiryu avatar May 01 '18 23:05 tkiryu