Ramu
Ramu

Reputation: 353

C# winforms datetime picker maxdate

The max date accepted by Datetime data type in SQL Server and C# seems to be 31/12/9999 23:59:59. I have tried to assign a data value (01/01/9999 00:00:00) from database to a datetime picker. It failed saying

DateTimePicker does not support dates after 31/12/9998 00:00:00.
Parameter name: MaxDate

Now my question is when both the datatypes are compatible (datetime), why does these is restriction in Datetimepicker. This seems to be by design. Can any let me know why this restriction and how this feature in design is going to help?

Thank you

Upvotes: 1

Views: 3406

Answers (2)

ChrisF
ChrisF

Reputation: 137148

In normal operations the date time picker has to be able to display dates after the selected date. This means that if you selected the max date (31st Dec 9999) then theoretically it would have to display 1st Jan 10,000 - which it can't.

Therefore the picker itself restricts the initial value to be some arbitrary value before the max date so subsequent dates can be shown.

I don't see that this is a problem. When, in a real world application, is the user going to want to set a date 7000+ years in the future?

If your application has unset dates then perhaps you need to consider storing them as null in the database (i.e. make the column nullable) so that it's presented as a blank on the UI. This may actually make it easier for the user to spot dates that haven't been set.

Upvotes: 2

Serve Laurijssen
Serve Laurijssen

Reputation: 9753

It's as simple as that DateTimePicker checks against

DateTimePicker.MaximumDateTime

which is defined as december 31 9998

here's the relevant portion from the referencesource

public DateTime Value {
            set {
                bool valueChanged = !DateTime.Equals(this.Value, value);
                // Check for value set here; if we've not set the value yet, it'll be Now, so the second
                // part of the test will fail.
                // So, if userHasSetValue isn't set, we don't care if the value is still the same - and we'll
                // update anyway.
                if (!userHasSetValue || valueChanged) {
                    if ((value < MinDate) || (value > MaxDate)) {
                        throw new ArgumentOutOfRangeException("Value", SR.GetString(SR.InvalidBoundArgument, "Value", FormatDateTime(value), "'MinDate'", "'MaxDate'"));
                    }
}

And MaxDate checks DateTimePicker.MaximumDateTime

http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DateTimePicker.cs,040fca665238ae30

Upvotes: 2

Related Questions