Question

I'm currently using .NET's DateTime's ToLongDateString() method to parse a date string. For some cultures this works fine:

US: Wednesday, May 16, 2001

But for a few other cultures the day name is omitted (examples are Dutch, Hungarian and Icelandic cultures).

NL: 16 mei 2001

The DayNames arrays of these cultures contain the proper names for every day of the week, but for some reason they aren't used in a long date string.

I tried using the DateTimeFormat.DayNames [i] + "D" formatting solution, but it didn't work here because it would lead to double day names on cultures that do already show the day name.

 US: Wednesday Wednesday, May 16, 2001
 NL: woensdag 16 mei 2001

Is there any way to make the day name appear for cultures that omit it by default?

Était-ce utile?

La solution

Thats correct.

.NET does what it should do, if you have a look at the regional settings of a windows pc you can change the culture and see whats displayed in the Date (long) field and you will see the following:

for Dutch: d. MMMM YYYY

for Hungarian: YYYY. MMMM d.

for Icelandic: d. MMMM YYYY

You will have to add it manually if you really like to have it for all of them, or force a certain format layout with

DateTime.Now.ToString("dddd, dd MM YYYY");

Have a look at this page to see where the regional settings are found on a windows 7 pc: http://windows.microsoft.com/en-us/windows7/change-the-country-or-region-setting

Autres conseils

Many cultures have multiple long date patterns and you can select from them the first one which contains a day of the week pattern:

    static void Main(string[] args)
    {
        foreach (var cultureInfo in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures))
        {
            string longDateWithDayOfWeek = null;
            foreach (var pattern in cultureInfo.DateTimeFormat.GetAllDateTimePatterns('D'))
            {
                if (pattern.Contains("ddd"))
                {
                    longDateWithDayOfWeek = pattern;
                    break;
                }
            }

            bool isFallbackRequired = string.IsNullOrEmpty(longDateWithDayOfWeek);
            if (isFallbackRequired)
            {
                longDateWithDayOfWeek = "dddd, " + cultureInfo.DateTimeFormat.LongDatePattern;
            }
            System.Console.WriteLine("{0} - {1} {2}", cultureInfo.Name, longDateWithDayOfWeek, (isFallbackRequired) ? " (generated)" : string.Empty);
        }
    }

Dutch and Icelandic are supported this way, while Hungarian will require a little more research on your part to override correctly.

If you think about what you're asking - it will be clear you'll need to check, and add it yourself.

You're basically saying "If culture X doesn't contain the day name, add it" which translates to:

var date = DateTime.Now;

if(!date.ToLongDateString().Contains(date.ToString("dddd"))
    //Add it
DateTime dt = new DateTime(2001, 5, 16);
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("nl-NL");
Debug.WriteLine(dt.ToLongDateString());

// output:  woensdag 16 mei 2001

I suppose that other cultures might have formats that don't contain the day of week name, but the one you provided does - at least on my machine.

One thing to note, if you're going to check and add it yourself, are you sure that it should always be at the front of the string? In many cultures it is, but perhaps not all of them work that way. Also, what about the separator? Just a space is used here, but many use a comma and a space. Others could use something different.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top