Click here to Skip to main content
14,932,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to fix minimum and maximum date in datetimepicker(windows application)in c#.
do we can select any year and day but the restriction only in month.
ex: if we select year 2017, then month can be restricted from 2017 april to 2018 march.

What I have tried:

int year = dateTimePicker1.Value.Year;
dateTimePicker1.MinDate = new DateTime(year,04, 01);
dateTimePicker1.MaxDate = new DateTime(year+1, 03, 31);

but this code get error,how can we select different year from here,
plz help
Updated 28-Aug-18 23:58pm
OriginalGriff 29-Aug-18 3:54am
What error? When?
Member 13854008 29-Aug-18 4:31am
actually here the year came only the current year, no other year can be selected.
OriginalGriff 29-Aug-18 4:39am
We can't see your screen, we can't access your HDD, and we can't read your mind.
So when you tell us "this code get error" it tells us nothing about what might be going on, because we only get exactly what you type to work from - we get no other context at all!
And adding just "actually here the year came only the current year, no other year can be selected." doesn't tell us much: we have no idea what the sequence of events was, what happened and when it happened, and what you did to make that happen.

So please, stop trying to type as little as possible, and help us to help you!
Member 13854008 29-Aug-18 4:59am
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)

int year = dateTimePicker1.Value.Year;

dateTimePicker1.MinDate = new DateTime(year,04, 01);
dateTimePicker1.MaxDate = new DateTime(year+1,03,31);

this is my code.if i select other year from datetimepicker,its get an error as(Value of '17/04/01 12:00:00 AM' is not valid for 'MinDate'. 'MinDate' must be less than MaxDate and it only select system year.)
Richard Deeming 30-Aug-18 11:20am
So you want to restrict the user to select a date between 1st April and 31st March, and allow them to change the year as well?

In other words, you want to allow them to select any date.

1 solution

Why you are calling this in the ValueChanged handler?
It might not work as expected because it is changed everytime the user changes one of the picker fields.

However, the error message is quite clear:
The passed date is after the actual MaxDate setting.

This can be avoided by setting MinDate first to MinDateTime, then MaxDate, and finally the new MinDate to be used:
dateTimePicker1.MinDate = DateTimePicker.MinDateTime;
dateTimePicker1.MaxDate = new DateTime(year+1,03,31);
dateTimePicker1.MinDate = new DateTime(year,04, 01);
Member 13854008 29-Aug-18 6:14am
thanku sir that error has solved ,but how this year assign here
int year = dateTimePicker1.Value.Year; //this code only select system year.
I want select all years years here. plz help thats too sir.
Jochen Arndt 29-Aug-18 6:23am
That line gets the actually selected year from the control. If you have not set that from your code, the picker will be initialised with the current date upon creation.

It is unclear to me what you finally want to achieve. If you want the user to specify a date range, you have to use two controls:
One for the start date and one for the end date.
Member 13854008 29-Aug-18 7:03am
sir ,then how could that control given in the code.

dateTimePicker1.MinDate = DateTimePicker.MinDateTime;
dateTimePicker1.MaxDate = new DateTime(year+1,03,31);
dateTimePicker1.MinDate = new DateTime(year,04, 01);

actually in this code year is given as year no other other value given, so we can assign all years in a variable(year)( otherwise year seems red error). so how could all years assign in that variable (year)
Jochen Arndt 29-Aug-18 7:12am
What do you mean by "all years"?
A datetime picker allows selecting a single datetime only. An int variable like 'year' can also hold only a single value. If you need a range, use two variables / controls.

The MinDate and MaxDate members are limiting the allowed input range for that single date. If that is what you want, set them accordingly.
Member 13854008 29-Aug-18 7:42am
sir, all years means not select all years at a time, but when we select one year by mistakened ,we can select another year there ,how it can possible.
sir please give hint too.
Jochen Arndt 29-Aug-18 7:53am
I still don't understand.

A user can select any date within the limit and change his selection until the control is closed. It just depends on when your code reads the current selection. When the control is part of a dialog, that is usually done in the OK button handler.

You are using actually the ValueChanged handler of the picker control. That is not the right place to read the selection because you don't know if it is the final value.
Member 13854008 29-Aug-18 8:00am
ok sir, I changed the value changed handler, then how could that limit or range given in that code,or how could currently select different years
Jochen Arndt 29-Aug-18 8:15am
Assuming you have a dialog containing the picker:

- Upon creation of the dialog, set the limits. I can't help with that because I don't know them.

- In the OK button handler read the current value. Check the value optionally (usually not necessary).

To give more help I have to know what you finally want: what kind of dates the user can set, where the control is located (like a dialog), and how end of user input is signaled (e.g. an OK / Apply button).

All the above are design steps which have to be done before writing any line of code.
Member 13854008 31-Aug-18 1:18am
private void button2_Click(object sender, EventArgs e)
int year = int.Parse(comboBox1.Text);
dateTimePicker1.MinDate = DateTimePicker.MinDateTime;
dateTimePicker1.MaxDate = new DateTime(year + 1, 03, 31);
dateTimePicker1.MinDate = new DateTime(year, 04, 01);
Here I used //int year = int.Parse(comboBox1.Text);//
this code for select the year.But this year select in combobox at running time.
but user want to select year from datetimepicker itself at running time.
ex:if user select 2014 datetimepicker, it shows from 2014 april to 2015 march .
then if user select another(2016) year from datetimepicker ,it shows 2016 april to 2017 march, here the month only restrict from april to march
Jochen Arndt 31-Aug-18 2:58am
The shown code does not select a date in the picker. It only sets the range.

If you set the range that way when the user selects the year 2014, he can't select 2016 afterwards because that is out of the range.

Define first what the user can enter (the type of controls and what they contain) and what should happen with the entered data (get data and process them) on specific actions (controls changed and buttons activated). Once that is done you can think on how it can be implemented by code.

For me it looks like you are trying to do it the other way round:
Writing code without knowing what should be done.

I still don't know what you finally want to do. If you for example want to have a range of one year beginning at a start date, just use a datepicker without any adjustments. Once the user clicks the OK button, get the date and add one year in days to get the end date. If required, this can be then used in another picker to allow only selection within this range.
Member 13854008 31-Aug-18 5:06am
ok sir I got it and thank you very much for your consideration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900