Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
hii,
how to compare years between to datetimepickers such that there must be atleast 18 years of gap.
My code:
if (dateTimePicker1.Value.Date.CompareTo(dateTimePicker2.Value.Date) < 18)
           {
               MessageBox.Show("minimum age must be 18 years");
           }
 
Is there any solution?
Posted 11-Mar-13 0:24am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This should be enough: you can use the operators '==', '!=', '<=', '>=', '<', and '>' with System.DateTime.
 
—SA
  Permalink  
Comments
Ian A Davidson at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
// 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.
  Permalink  
Comments
Dev Gupta from Mumbai,India at 11-Mar-13 7:54am
   
Hey Thanks,its now working..
Ian A Davidson at 11-Mar-13 9:56am
   
Glad to hear it. All the best,
Ian.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this
 
if((DateTimePicker1.Value-DateTimePicker2.Value).Year < 18)
  Permalink  
v3
Comments
Dev Gupta from Mumbai,India at 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 at 11-Mar-13 6:04am
   
Are you setting datetimepicker value from code?
Ian A Davidson at 11-Mar-13 6:24am
   
You're comparing number of Days. The question was asking to compare Years.
Dev Gupta from Mumbai,India at 11-Mar-13 7:47am
   
I had tried to compare it with years,bt result was just the same....
pradiprenushe at 11-Mar-13 6:40am
   
@Dev Gupta from Mumbai,India Sorry my mistake. I have updated solution
Ian A Davidson at 11-Mar-13 7:01am
   
Subtract returns a TimeSpan which doesn't have a Years (or Year!) property, so I don't believe this will work, as I say in my solution below.

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

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 11 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100