`System.DateTime`

.12,064,615 members (26,330 online)

Email

Password

Sign in using

hii,

how to compare years between to datetimepickers such that there must be atleast 18 years of gap.

My code:

Is there any solution?

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?

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

—SA

`System.DateTime`

.Comments

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

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.

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

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

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

Regards,

Ian.

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.

Comments

Hey Thanks,its now working..

Glad to hear it. All the best,

Ian.

Ian.

Try this

`if((DateTimePicker1.Value-DateTimePicker2.Value).Year < 18)`

v3

Comments

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

Still messagebox is shown.I wrote this code in validating event of datetimepicker2

Are you setting datetimepicker value from code?

You're comparing number of Days. The question was asking to compare Years.

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

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)

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