Click here to Skip to main content
15,867,686 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
Posted
Updated 28-Aug-18 23:58pm
Comments
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    
:sigh:
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:
C#
dateTimePicker1.MinDate = DateTimePicker.MinDateTime;
dateTimePicker1.MaxDate = new DateTime(year+1,03,31);
dateTimePicker1.MinDate = new DateTime(year,04, 01);
 
Share this answer
 
Comments
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.

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