1

I found out that this line:

DateTime.ParseExact("Jan 30", "MMM yy", null, System.Globalization.DateTimeStyles.None)

creates the date:

2030-01-01

...on my Windows 10 machine, but on Windows Server 2012 R2 the output is:

1930-01-01

Does anybody know how to get Windows Server 2012 to parse the date as 2000 instead of 1900?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
kraeppy
  • 23
  • 4
  • 3
    The real solution is to stop using two-digit years. No matter what cutoff the system picks, some dates are going to end up wrong. If you're looking for consistency, counting on every system to use the same cutoff rules isn't the way to go. [See also](https://stackoverflow.com/q/58508724/4137916). – Jeroen Mostert Jun 23 '21 at 07:27
  • 1
    https://www.hanselman.com/blog/how-to-parse-string-dates-with-a-two-digit-year-and-split-on-the-right-century-in-c – Chetan Jun 23 '21 at 07:28
  • Unfortunately it's an exchange that publishes delivery times in this way -.- – kraeppy Jun 23 '21 at 07:53

1 Answers1

3

It's based on the culture's default calendar's TwoDigitYearMax property. If you change that property value, you'll get a different result:

using System;
using System.Globalization;

class Program
{
    static void Main(string[] args)
    {
        var culture = CultureInfo.CurrentCulture;
        
        // Clone returns a *mutable* copy.
        culture = (CultureInfo) culture.Clone();
        
        culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2029;
        var result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
        Console.WriteLine(result.Year); // 1930

        culture.DateTimeFormat.Calendar.TwoDigitYearMax = 2129;
        result = DateTime.ParseExact("Jan 30", "MMM yy", culture, System.Globalization.DateTimeStyles.None);
        Console.WriteLine(result.Year); // 2030
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Do you maybe know the answer for this one if you are familiar with Azure pipelins? https://stackoverflow.com/questions/68096837/azure-pipeline-custom-task-with-an-unknown-number-of-inputs – CodeMonkey Jun 23 '21 at 10:52
  • @YonatanNir: Please don't use comments to request help on unrelated questions, – Jon Skeet Jun 23 '21 at 15:00