Search code examples
c#date.net-7.0culture

Trouble with AbbreviatedMonthNames


It seems that the masses have spoken and basically the shorthand for September is now Sept.

So whilst Intl.DateTimeFormat Works on the principle (which my datepicker uses) my operating system has not got the memo (at present) and continues to work with Sep.

Anyway, so in an attempt to resolve this I've added in

CultureInfo ci = new CultureInfo("en-GB");

DateTimeFormatInfo dtfi = ci.DateTimeFormat;
dtfi.AbbreviatedMonthNames  = new  string[] {
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec", ""
            };
dtfi.AbbreviatedMonthGenitiveNames = dtfi.AbbreviatedMonthNames;

CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;

Into my program file and this does seem to have resolve the issue and - I'm hoping that when Windows 11 comes around (for me) I might not need that code.

The question I have is - is this the right solution? My application is only going to be used in the Uk, but I'm a little wary. I seem to have created a new culture and basically forced the program to use it.

Is this the right approach?

--

Edit, sorry I'll backup slightly - so when it comes to datepickers I like to use the format dd-MMM-yyyy. It's pretty standard where users/staff can come from all parts and just removes any "ambiguity" over date formats. Uk vs US for instance.


As for official "The masses have spoken" - TBH I might have spoken out of turn. However https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat https://github.com/unicode-org/cldr/blob/b0a6207ff224a6cd8ca7f888c8a4740bacb83124/common/main/en_GB.xml#L1977 https://github.com/dotnet/runtime/issues/87307

There seems to be a few discussions about it .. And someone, somewhere did make the call...

--

Just for further clarity I'm using the Tempus Dominus datepicker. https://getdatepicker.com/ Which is using the Intl.DateTimeFormat methods.


Solution

  • So I think some of this depends on your operating system and choice of datapicker. For me using https://getdatepicker.com/ and on windows 10. The right way to fix this is

    1. Install the Microsoft.ICU.ICU4C.Runtime via nuget
    2. Create the file runtimeconfig.template.json in the root of the project

    add in the content

    {
          "configProperties": {
            "System.Globalization.AppLocalIcu": "72"
          }
        }
    

    When you compile this it then goes into the runtimeconfig.json in the bin file. But that file can get overwritten (and probably is't source controlled if you are using such things.

    And your done. On the whole this is probably the best method espicially if you are using that datepicker and developing an application that could be used in various conntries. (and also using the short month format ; - "MMM")