3

In current Chrome, Firefox and Safari Intl.DateTimeFormat for the locale it-CH (italian part of Switzerland) is wrong:

new window.Intl.DateTimeFormat('it-CH').format(new Date()) // -> "6/7/2017"
new window.Intl.DateTimeFormat('fr-CH').format(new Date()) // -> "06.07.2017"
new window.Intl.DateTimeFormat('de-CH').format(new Date()) // -> "06.07.2017"

The output of the first line is wrong. In all parts of Switzerland the format should be "dd.mm.yyyy"

Interestingly IE11 & Edge produce the correct output for the above snippet.

What is the best way to fix/patch/override the faulty implementation of window.Intl in given browsers?

jbandi
  • 17,499
  • 9
  • 69
  • 81

1 Answers1

1

Not sure about best but the easiest should be something like this:

var nativeDateTimeFormat = window.Intl.DateTimeFormat;
window.Intl.DateTimeFormat = function(locale) {
  var native = nativeDateTimeFormat(locale);
  if (locale === 'it-CH') {
    native.format = function() {
      return nativeDateTimeFormat('fr-CH').format();
    }
  }
  return native;
}

This solution uses the fact that fr-CH has the correct format that it-CH should have.

Lennholm
  • 7,205
  • 1
  • 21
  • 30
  • Unfortunately this seems not to work in Chrome. Setting the `format` function on the native `DateTimeFormat` seems not possible. Setting it does simply have no effect ... – jbandi Jul 13 '17 at 08:45
  • I posted a follow-up question here: https://stackoverflow.com/questions/45077313/understanding-intl-datetimeformat-as-a-javascript-object – jbandi Jul 13 '17 at 09:59
  • @jbandi Ah, I see. Hopefully you could use the code from my suggestion in a utility function and use that, but if you're relying on a 3rd party library that uses `Intl.DateTimeFormat` internally that's of course not an option, unfortunately. – Lennholm Jul 13 '17 at 10:15