Behaviour of Convert.ToDateTime(string) function

  • Thread starter Thread starter Jack Wright
  • Start date Start date
J

Jack Wright

Dear All,
Behavior of Convert.ToDateTime(string) function:
If the string to be converted to DateTime is like this:
01.09.29, Convert.ToDateTime(string) function returns DateTime as
01.09.2029.
But if try with 01.09.30, then Convert.ToDateTime(string) function
returns DateTime as 01.09.1930

So if the string has two digit year less than 30, while converting
year, it just adds 2000 in that year,
i.e. Resulting Year = 'Two Digit Year' + 2000

But if two digit year >= 30, then
Resulting year = 'Two Digit Year' + 1900

It happens irrespective of my m/c date. Is this behaviour right?

Thanx & Regards
Jack
 
Hi Jack,

This is expected behaviour when using only two digits for year.
It is configured under Regional and Language options->Customize->Advanced.
You may be able to change this temporarily using a cultureinfo, but I am unsure how.
 
Hi Jack,

This information is stored under Calendar.TwoDigitYearMax

int lastyear = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar.TwoDigitYearMax;

You can easily change it to any value you want. Preferably in a local cultureinfo.
Default is 2029.
 
Back
Top