简体   繁体   中英

AbbreviatedMonthNames not working properly in .NET Core 3.1

I'm having a problem with custom DateTime in .NET Core 3.1. As far as I can tell, my code should really work. I've done nothing particular complicated, but just set custom month names.

I want a Swedish culture, and months to be printed without the dot at the end.

using System;
using System.Globalization;

namespace Playground.DateTime
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var culture = new CultureInfo("sv-SE")
            {
                DateTimeFormat =
                {
                    AbbreviatedMonthNames = new [] { "jan", "feb",  "mar" ,"apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec", ""}
                }
            };

            var today = new System.DateTime(2020, 01, 16);

            Console.WriteLine($"Today date without culture set: {today.ToString("d MMM yyyy")}");

            CultureInfo.DefaultThreadCurrentCulture = culture;
            CultureInfo.DefaultThreadCurrentUICulture = culture;

            Console.WriteLine($"Today date WITH culture set: {today.ToString("d MMM yyyy")}");

            Console.ReadLine();
        }
    }
}

Output from above code is:

Today date without culture set: 16 Jan 2020

Today date WITH culture set: 16 jan. 2020

I would expect it to be (note without the dot on the second output):

Today date without culture set: 16 Jan 2020

Today date WITH culture set: 16 jan 2020

Here's a dotnetfiddle for the above code.

Any ideas on what's going on? Thanks!

What I was missing was to set another property, namely AbbreviatedMonthGenitiveNames . Here's a working dotnet fiddle . If you're curious you can read more here .

Not sure but I guess this behaviour is different between .NET Core 2.X and 3.X, since I haven't experienced this issue before. Been running .NET Core 2.2 for quite some time and all of a sudden when upgrading to .NET Core 3.1 this showed up as an issue.

UPDATE

Previously there was a bug in CoreCLR ( https://github.com/dotnet/corefx/issues/2804 ) that was fixed in this PR ( https://github.com/dotnet/coreclr/pull/26384 ). So now you must set AbbreviatedMonthGenitiveNames as mentioned by Tarek.

We had a reported bug https://github.com/dotnet/corefx/issues/2804 in formatting genitive names (eg when using d MMM formats). We have fixed this bug to correctly use the genitive month names and not just the abbreviated month names.

You can fix your code by adding the second line in the snippet:

AbbreviatedMonthNames = new [] { "jan", "feb",  "mar" ,"apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec", ""}

AbbreviatedMonthGenitiveNames = AbbreviatedMonthNames;

For more information, you may consult the doc https://docs.microsoft.com/en-us/dotnet/api/system.globalization.datetimeformatinfo.abbreviatedmonthgenitivenames?view=netframework-4.8#System_Globalization_DateTimeFormatInfo_AbbreviatedMonthGenitiveNames

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM