1

I want to print out a date with a 4 digit year. This should be very simple, according to MS docs. But

    static void Main(string[] args)
    {
        var sample = new DateTime(2050, 1, 1);
        var datestr = sample.ToString("d", CultureInfo.GetCultureInfo("de"));
        Console.WriteLine(datestr);
    }

prints 01.01.50, instead of 01.01.2050. Why? This is .Net Core 2.1 on a Mac. See also https://dotnetfiddle.net/4ZSbOJ

mjwills
  • 23,389
  • 6
  • 40
  • 63
Frank Hintsch
  • 560
  • 8
  • 14
  • I can't replicate that: https://dotnetfiddle.net/BzO3SO. See also https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#the-short-date-d-format-specifier. – Igor Jul 03 '19 at 13:46
  • Might be a system setting somewhere to choose to display two digit years versus 4. That will effect the culture setting for stuff like this as well. – Chris Pratt Jul 03 '19 at 13:54
  • What is the date and time shown in the bottom right of your screen? https://www.howtogeek.com/247141/how-to-change-the-format-of-dates-and-times-in-windows/ – mjwills Jul 03 '19 at 14:10
  • Try it with dotnetfiddle.net and .Net Core 2.2 compiler: https://dotnetfiddle.net/4ZSbOJ – Frank Hintsch Jul 03 '19 at 14:31
  • Frank is right, the output is different, will have to investigate why using different .Net framework renders it differently – Khan Jul 03 '19 at 15:06

2 Answers2

3

Answer taking into account this happens on MacOS

After digging around the source code a bit it seems that on Linux/MacOS .NET Core will use ICU for date and time formatting and in your case this results in a two digit year. There is an issue resolved by a pull request to make the year formatting consistent across platforms. However, as far as I can see this change is only scheduled to be included in .NET Core 3.


Wrong answer assuming the OS is Windows

Executing the following code

var sample = new DateTime(2050, 1, 1);
var datestr = sample.ToString("d", CultureInfo.GetCultureInfo("de"));
Console.WriteLine(datestr);

prints

01.01.2050

so it seems that the problem is specific to you.

However, while you have specified that your culture is "de_DE" your code doesn't use a culture. When no culture is specified in the ToString call then CultureInfo.CurrentCulture is used so I assume that you have configured Windows to use German regional format.

Windows allows you to customize the regional format and if you do that .NET will use your modified format when CultureInfo.CurrentCulture is used. You can inspect and modify the format in the Windows Settings app:

  • Settings (Windows key + I)
  • Time & Language
  • Region
  • Change data formats

You might find this surprising and there is an even more surprising fact about the culture returned by CultureInfo.CreateSpecificCulture that you can read about in my answer to the question When to use CultureInfo.GetCultureInfo(String) or CultureInfo.CreateSpecificCulture(String).

To avoid this feature (or quirk) of CultureInfo.CurrentCulture you can explicitly specify a CultureInfo when you format the date. However, if this date is displayed to the user then it's probably better to just use CultureInfo.CurrentCulture to allow the user to freely customize the format.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
1

You are right the .NET Core 2.2 renders the date differently vs .NET 4.7.2. For now you could use

var datestr = sample.ToString("dd.MM.yyyy", CultureInfo.GetCultureInfo("de-DE"));

Until we find out what is happening, it is either a bug or a feature. Just validate the day and month order.

Just go look at format with .Net Core and the output does not match in .Net Core 2.2.

d Format Specifier      de-DE Culture                                 01.10.08
d Format Specifier      en-US Culture                                  10/1/08
d Format Specifier      es-ES Culture                                  1/10/08
d Format Specifier      fr-FR Culture                               01/10/2008

Expected

d Format Specifier      de-DE Culture                               01.10.2008
d Format Specifier      en-US Culture                                10/1/2008
d Format Specifier      es-ES Culture                               01/10/2008
d Format Specifier      fr-FR Culture                               01/10/2008

This is a problem

var sample1 = new DateTime(2013, 1, 1);
var sample2 = new DateTime(1913, 1, 1);

Renders the same output

Khan
  • 516
  • 11
  • 24
  • .NET Core 2.2 renders dates in the same way .NET 4.7.2 on **Windows**. The problem is that .NET Core 2.2 renders them differently on Windows and Linux (full framework .NET is not available on Linux). – Martin Liversage Jul 03 '19 at 15:09