Reputation: 129
so i have a code that converts 3 numbers separated by "/" into the earliest possible date. so 9/22/12 is 2012-Sept-22. however some dates show up as early as 1933. i would like to know how to keep the dates limited to after the year 2000. this is the code that does the date print out:
String v = Convert.ToString(year);
String x = Convert.ToString(mon);
String w = Convert.ToString(day);
String z = v + "-" + x + "-" + w;
DateTime fg;
if (DateTime.TryParse(z, out fg)){
String hh = fg.ToString();
DateTime dt = DateTime.Parse(hh, cultureinfo);
Console.Write(dt);
}
Upvotes: 1
Views: 1293
Reputation: 150108
Back when the Year 2000 Problem hit, people were scrambling to figure out how to deal with two-digit dates in older systems that never allocated space for the full four digits. If possible, I would suggest you avoid two-digit dates in your own system.
One technique for handling the existing two-digit dates is to introduce a concept called date windowing, where lower-number dates would be interpreted as the 2000's and higher number dates would be interpreted as the 1900's. This worked in many instances, but resulted in a 105 year old lady getting an invitation to register for Kindergarden (along with many other issues).
This is happening here.
You can control the cutoff that is used for deciding if a two-digit year is the current or previous century using Calendar.TwoDigitYearMax
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.
Upvotes: 2