-5

I'm going to have two dates. A Date of birth And a Date of event

I want to calculate if the time passed between date of birth and event date is less than or greater than 18 years. The event date will always be greater than the date of birth.

If under 18 then true else false.

EG: DOB 29/02/1976. Event Date 29/12/2017

The person here is over 18!

I'm not sure if using DateAdd or DateDiff is the best approach - or even if using DateTime.Days && DateTime.Months etc

Martin
  • 55
  • 1
  • 8
  • 1
    Can you show some code of your current efforts, that would help us helping you! – Chrille Jan 29 '18 at 11:13
  • 1
    The only tricky thing is a *leap year*: what is the threshold date, is it `28 Feb` or `1 Mar`? – Dmitry Bychenko Jan 29 '18 at 11:15
  • `if (DOB.AddYears(18) >= EventDate) {...}` – Dmitry Bychenko Jan 29 '18 at 11:15
  • @DmitryBychenko both `TimeSpan` and `DateTime` take leap years into account. – Adwaenyth Jan 29 '18 at 11:17
  • best way to approach this may depend on whether this is a one-off comparison between one DOB and one event or whether you're working with *lists* of items with one or the other of these data items. – Damien_The_Unbeliever Jan 29 '18 at 11:17
  • @Adwaenyth - yes, they do. But they have to apply an *arbitrary* rule to the logic, which may not fit with what an individual person/company wants to apply in their circumstances. At the least, the OP ought to review the rule that those types implement and see if it conforms with their expectations. – Damien_The_Unbeliever Jan 29 '18 at 11:18
  • 1
    @Adwaenyth: TimeSpan has no concept of a leap year in itself, because it only deals with elapsed time - it has no concepts of months at all. The threshold Dmitry talked about is important, as shown in my answer. – Jon Skeet Jan 29 '18 at 11:22

1 Answers1

8

The example you gave is a reasonably easy one - but there are harder ones. Consider this:

  • Date of birth: Feb 29th 1976
  • Event date: Feb 28th 1994

If you add 18 years to the date of birth, you'll get Feb 28th 1994 - the event date. That suggests the person is 18 years old.

If you subtract 18 years from the event date, you'll get Feb 28th 1976 (before the person's birthday). That suggests the person isn't 18 years old.

I suspect in almost all cases, you'd consider that the person celebrates their birthday on Feb 28th in non-leap years. So the code would be:

var adult = birthDate.AddYears(18) <= eventDate;

Make sure both values are just dates without any time-of-day though.

The core .NET libraries don't provide any way of finding the period between two DateTime values. You can find the TimeSpan, but that's not the same thing, and isn't useful for calendrical calculations. (You can't tell whether a TimeSpan of 30 days is a whole month or not for example, because it depends on the context.)

My Noda Time library does provide this facility:

var birthDate = new LocalDate(1976, 2, 29);
var eventDate = new LocalDate(1994, 2, 28);
var period = Period.Between(birthDate, eventDate);
var adult = period.Years >= 18;
Console.WriteLine(period); // P18Y

... but you can easily accomplish what you need in DateTime in this case. (I'd advise using Noda Time anyway if you have other date/time scenarios, just on the principle that it's designed to make date/time work easier to get right.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    "Close counts in darts, horseshoes and nuclear war - not when trying to determine if a person is above 18 years!" Working with Dates is the 2nd most tricky thing in Programming. Only working with natural langauge is worse. – Christopher Jan 29 '18 at 11:21