Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.ParseExact - why yy turns into 2015 not 1915

Why does .NET assume that from following we mean year as 2015, not 1915.

var d = DateTime.ParseExact("20/11/15", "dd/MM/yy", new CultureInfo("en-GB"));

I guess, it tries proximity, but is there a reasonable design decision behind it?

like image 747
Yahya Avatar asked Nov 20 '15 14:11

Yahya


People also ask

What does DateTime ParseExact do?

ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

How to Parse date format in c#?

TryParse() always try to parse the string value datetime. If conversion succeeded then it returns correct DateTime value and MinValue(1/1/0001 12:00:00 AM) if conversion failed. If string value is null or empty and you are trying to convert it DateTime then it returns MinValue only.

How do I convert a string to a date C#?

In C#, a string can be converted to DateTime object using parsing methods provided by DateTime struct. Apart from these methods, we can convert a string to date using Convert. To DateTime() method which takes a string representation of a date as input and returns its equivalent DateTime object.


2 Answers

This is a system setting you can customize for your locale if you want:

enter image description here

like image 107
Joey Avatar answered Nov 05 '22 18:11

Joey


It uses Calendar.TwoDigitYearMax property.

This property allows a 2-digit year to be properly translated to a 4-digit year. For example, if this property is set to 2029, the 100-year range is from 1930 to 2029. Therefore, a 2-digit value of 30 is interpreted as 1930, while a 2-digit value of 29 is interpreted as 2029.

For en-GB the value of this property is 2029. It can be changed in Control Panel:

The initial value of this property is derived from the settings in the regional and language options portion of Control Panel.

From MSDN:

In a parsing operation, a two-digit year that is parsed using the "yy" custom format specifier is interpreted based on the Calendar.TwoDigitYearMax property of the format provider's current calendar.

like image 31
Jakub Lortz Avatar answered Nov 05 '22 17:11

Jakub Lortz