Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii,
how to compare years between to datetimepickers such that there must be atleast 18 years of gap.
My code:
C#
if (dateTimePicker1.Value.Date.CompareTo(dateTimePicker2.Value.Date) < 18)
           {
               MessageBox.Show("minimum age must be 18 years");
           }


Is there any solution?
Posted

This should be enough: you can use the operators '==', '!=', '<=', '>=', '<', and '>' with System.DateTime.

—SA
 
Share this answer
 
Comments
Ian A Davidson 11-Mar-13 10:47am    
"Value" is a System.DateTime!
I expect you can indeed indeed use the operators, but in order to find out the number of Years difference, your options are a bit more limited because - unless you know something I don't - TimeSpan doesn't do years!

So you still either need to do what I suggested (add 18 years to the earlier DateTime) or use the more long-winded traditional route of comparing Year, then Month then Day, of the fashion:

int deltaY = y2 - y1;
if ((deltaY < 18) ||
((deltaY == 18) && ((m2 < m1) || ((m2 == m1) && (d2 < d1))))
{
// Warn
}

Regards,
Ian.
Sergey Alexandrovich Kryukov 11-Mar-13 11:29am    
I can see your point, it's correct. I mostly responded to you "CompareTo". The use of the operator leaves less room for a bug, more readable...
—SA
First of all, Value is the DateTime, so you don't need the ".Date" on the end.

Secondly, CompareTo just gives a boolean result to tell you if one date is earlier than the other.

Subtracting one date from the other results in a TimeSpan, which gives difference in days, hours, minutes... Not very helpful, obviously, because years are unfortunately not all the same length.

So the easiest solution would be to add 18 years to the earlier date.

C#
// Add 18 years to earlier date, and then compare it to the later date.
// Note we check to see if the result is greater than zero because
// if the difference is less than 18 years,
// the earlier date will now in fact be later.
if (dateTimePicker1.Value.AddYears(18).CompareTo(dateTimePicker2.Value) > 0)
{
	MessageBox.Show("minimum age must be 18 years");
}


Disclaimer: I've not tested this, so you may have to fix any typos yourself!

Regards,
Ian.
 
Share this answer
 
Comments
Dev Gupta from Mumbai,India 11-Mar-13 7:54am    
Hey Thanks,its now working..
Ian A Davidson 11-Mar-13 9:56am    
Glad to hear it. All the best,
Ian.
Try this

C#
if((DateTimePicker1.Value-DateTimePicker2.Value).Year < 18)
 
Share this answer
 
v3
Comments
Dev Gupta from Mumbai,India 11-Mar-13 5:56am    
Its not working.In datetimepicker1 I took year 1990 & in datetimepicker2 I took year 2013.
Still messagebox is shown.I wrote this code in validating event of datetimepicker2
pradiprenushe 11-Mar-13 6:04am    
Are you setting datetimepicker value from code?
Ian A Davidson 11-Mar-13 6:24am    
You're comparing number of Days. The question was asking to compare Years.
Dev Gupta from Mumbai,India 11-Mar-13 7:47am    
I had tried to compare it with years,bt result was just the same....
pradiprenushe 11-Mar-13 6:40am    
@Dev Gupta from Mumbai,India Sorry my mistake. I have updated solution

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