2

I am using datetimepicker which displays the date(which is retrieved from the database). I have run my application on a few machines and I have found that on Windows XP SP2, the max date that can be retrieved and displayed is 2020, but for later versions of the OS the max date can be highier, e.g. 2999. Does anyone know about this issue?

Thank you for your help. Jing Jing

JingJingTao
  • 1,760
  • 17
  • 28

1 Answers1

2

You can use the DateTime.MaxValue property to retrieve the largest possible date, dependent on the system. If you are having problems reading dates due to overflow, you can use the DateTime.TryParse() methods to attempt a read of the value without throwing an exception.

UPDATE: Martinho is correct. From MSDN:

Some calendars, such as the UmAlQuraCalendar, support an upper date range that is earlier than MaxValue. In these cases, trying to access MaxValue in variable assignments or formatting and parsing operations can throw an ArgumentOutOfRangeException. Rather than retrieving the value of DateTime.MaxValue, you can retrieve the value of the specified culture's latest valid date value from the System.Globalization.CultureInfo.DateTimeFormat.Calendar.MaxSupportedDateTime property.

So, use the System.Globalization.CultureInfo.DateTimeFormat.Calendar.MaxSupportedDateTime field to get the maximum date value supported by the system.

Dave Swersky
  • 34,502
  • 9
  • 78
  • 118
  • 1
    Sorry, but no, DateTime.MaxValue is not dependent on the system. DateTime.MaxValue is 23:59:59.9999999, December 31, 9999, aka, 1 nanosecond before 00:00:00, January 1, 10000, aka, 3155378975999999999 ticks. – R. Martinho Fernandes Dec 22 '10 at 17:33
  • Also, if you're storing the DateTime in the database, make sure your date ranges are compatible. SQL Server's DateTime has a range of January 1, 1753 through December 31, 9999, for example, which is not the same as the C# `DateTime` type. – Jacob Dec 22 '10 at 17:36
  • 1
    To add to Jacob's comment: though the upper limit for SQL Server's and C#'s DateTime types is the same, the lower bound for C#'s DateTime is 00:00:00, January 1, 1, and so you can have values that you cannot store in SQL Server databases. However, I doubt many will ever run into the problem of not being able to store dates before 1753. – R. Martinho Fernandes Dec 22 '10 at 17:45