Calendar.TwoDigitYearMax Property
Gets or sets the last year of a 100-year range that can be represented by a two-digit year.
Syntax
[_Attribute]
public int TwoDigitYearMax { get; set; }
Property Value
The last year of a 100-year range that can be represented by a two-digit year. The default is 2029.
Remarks
The TwoDigitYearMax property is used to interpret a two-digit year as a four-digit year. For example, if TwoDigitYearMax is set to 2029, then the input "01" is interpreted as 2001, and the input "31" is interpreted as 2031. If the input is "30", it is interpreted as 1930.
The value of TwoDigitYearMax must be at least 100 years greater than the current year.
When parsing a two-digit year, the DateTime.Parse method and the DateTime.ParseExact method use the TwoDigitYearMax property to determine the appropriate century. The two-digit year is added to the year that is 100 years prior to TwoDigitYearMax. For example, if TwoDigitYearMax is 2029, and the input is "25", the year is calculated as 2029 - 100 + 25 = 2025.
Example
The following example demonstrates how to get and set the TwoDigitYearMax property and how it affects the interpretation of two-digit years.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Get the current TwoDigitYearMax value
int currentTwoDigitYearMax = DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax;
Console.WriteLine($"Current TwoDigitYearMax: {currentTwoDigitYearMax}");
// Set TwoDigitYearMax to a different value
DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax = 2099;
Console.WriteLine($"New TwoDigitYearMax: {DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax}");
// Parse a two-digit year
string dateString = "05-01-25"; // Represents May 1st, 2025
DateTime dt = DateTime.Parse(dateString);
Console.WriteLine($"Parsed '{dateString}' as: {dt}");
// Reset TwoDigitYearMax to its original value
DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax = currentTwoDigitYearMax;
Console.WriteLine($"Restored TwoDigitYearMax: {DateTimeFormatInfo.CurrentInfo.Calendar.TwoDigitYearMax}");
}
}