开发者

How to set a dateTimePicker value to DateTime.MaxValue

开发者 https://www.devze.com 2022-12-09 04:02 出处:网络
This generates an error开发者_开发知识库 at runtime: dateTimePicker.Value = DateTime.MaxValue;

This generates an error开发者_开发知识库 at runtime:

dateTimePicker.Value = DateTime.MaxValue;


You can't.

The maximum date supported by DateTimePicker is DateTimePicker.MaximumDateTime, which is 12/31/9998; DateTime.MaxValue is 12/31/9999 23:59:59, which is one year and one day later.

Can you use that DateTimePicker.MaximumDateTime instead of DateTime.MaxValue?


You need to use the DateTimePicker.MaximumDateTime property. The maximum value allowable for the datetime picker is 31/12/9998, as represented by DateTimePicker.MaximumDateTime. Whereas the value of DateTime.MaxValue is 31/12/9999.


Yes you can, but it is quite dirty (use it at your own risk). Basically, it overwrites the MaxValue defined in the DateTimePicker with the MaxValue from the DateTime object.

Paste this code into the Main (or any method run during startup):

var dtpType = typeof(DateTimePicker);
var field = dtpType.GetField("MaxDateTime", BindingFlags.Public | BindingFlags.Static);
if (field != null)
{
    field.SetValue(new DateTimePicker(), DateTime.MaxValue);
}


Perhaps this is helpful:

The value of this constant is equivalent to 23:59:59.9999999, December 31, 9999, exactly one 100-nanosecond tick before 00:00:00, January 1, 10000.

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.

blatantly copied from msdn

0

精彩评论

暂无评论...
验证码 换一张
取 消