6

The following piece of code gets the month from a date object in JavaScript.

const date = new Date(dateValue);
const month = date.toLocaleString('default', { month: 'short' });

For example: if the date is something like 30/07/2019 it will return Nov.

This works fine in Chrome but fails in Edge browser with error:

SCRIPT5121: SCRIPT5121: Locale 'default' is not well-formed

My Edge browser version is 41.16299.1004.0

Here's a jsfiddle: https://jsfiddle.net/1dwcv9xu/1

As per MDN, date.toLocaleString is fully supported in Edge: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString#Browser_compatibility.

Also I couldn't find this error code in the MSDN docs for Edge: https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide/console/error-and-status-codes.

Is there a way to fix this or any alternate approach to get the month in mmm format?

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78

5 Answers5

3

As per the suggestion from Phuzi, i changed to

date.toLocaleString('en-GB', { month: 'short' })

then it started working in IE 11 But it doesn't work in IE 10. So i went with Classic Javascript approach

3

Might seem crazy, but using undefined rather than default resolves the error in IE11 and works in all the major browsers that I've checked (Mac/Windows).

const date = new Date(dateValue);
const month = date.toLocaleString(undefined, { month: 'short' });
Chris Paul
  • 180
  • 1
  • 4
1

The arguments locales and options are not supported by all browser versions. Newer versions of Edge already support the "default" value, but older versions do not (despite supporting the parameters). I am not sure which version started to support the "default" value.

According to this page, "if the locales argument is not provided or is undefined, the runtime's default locale is used". Thus you could try date.toLocaleString(undefined, { month: 'short' });. Such value is supported by Edge.

The topic requires some more research. I stopped now once you answered the question with another solution. But if you have some more time give a try and share back your results with us.

saulotoledo
  • 1,737
  • 3
  • 19
  • 36
1

Answering this for completeness.

In order to use the default locale with the options param, you should always pass an empty array or undefined to the locales parameter.

The locales param for toLocaleString() follows Intl.DateTimeFormat. From MDN:

To use the browser's default locale, pass an empty array

Passing undefined will have the same effect as using an empty array.

Passing the string "default" works in most modern browsers, but so will "defaul" or any invalid locale. This is because most browsers will fall back on the default locale if they can't resolve it; this is not guaranteed to work and should not be expected to work, since it's incorrect usage.

Additionally, the options param is ignored entirely in older browsers, but the locales param will still be parsed correctly if both are used.

TonyArra
  • 10,607
  • 1
  • 30
  • 46
0

Since I didn't find a solution to the problem I have used an alternate approach which probably will work in all browsers:

const date = new Date(dateValue);
//const month = date.toLocaleString('default', { month: 'short' });
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
const month = monthNames[date.getMonth()];

Credits: Get month name from Date

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • One of the advantages of using `toLocaleDateString` is the localised names. Your solution is essentially the same as `date.toLocaleString('en-GB', { month: 'short' })` or `date.toLocaleString('en-US', { month: 'short' })` – phuzi Jul 30 '19 at 10:52
  • @phuzi I tried your approach but it only returns some digits like: 1,564,484,065,517 – Souvik Ghosh Jul 30 '19 at 10:56
  • I can see that you are using Edge 41. Which is older version. If possible for you than you can update with the latest version of Edge will solve your issue and your code is working fine with the latest version of MS Edge. – Deepak-MSFT Jul 30 '19 at 12:24