12,394,701 members (84,958 online)
Rate this:
See more:
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 10-Mar-13 23:24pm

Rate this:

## Solution 3

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

—SA
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
Rate this:

## 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.
{
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.
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.
Rate this:

## Solution 1

Try this

`if((DateTimePicker1.Value-DateTimePicker2.Value).Year < 18)`
v3
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

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....

@Dev Gupta from Mumbai,India Sorry my mistake. I have updated solution
Ian A Davidson 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.

Top Experts
Last 24hrsThis month
 OriginalGriff 305 Richard Deeming 269 ppolymorphe 245 Richard MacCutchan 124 Dave Kreskowiak 113
 OriginalGriff 5,783 Karthik Bangalore 2,337 ppolymorphe 2,290 F-ES Sitecore 1,852 Richard MacCutchan 1,627