Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare DateTime without year

Tags:

c#

datetime

I'm trying to get an alert when a Customer has their birthday within the next 7 days.

I've tried it with this code:

public bool IsBirthdayImminent
{
    get { return DateOfBirth != null && DateOfBirth.Value.Date >= DateTime.Today.Date.AddDays(-7); }
}

Of course this doesn't work, as the Date is stored with its year (say 05/21/1980) and it also compares the year. So this query will never be true - well, not if you're born within the next seven days though.

How can I modify this query to ignore the year?

Edit:

Alright, the query itself is not a problem at all. My primary point is the handling of leap years and situations around December <-> January.

like image 222
SeToY Avatar asked Jun 05 '13 19:06

SeToY


2 Answers

I would suggest using the following code. This includes cases around December - January and February, 29th. Though you might want to take a look and correct February 28th to be included or excluded within the given days.

    BirthdayImminent(new DateTime(1980, 1, 1), new DateTime(2012, 1, 2), 7); // false
    BirthdayImminent(new DateTime(1980, 1, 1), new DateTime(2012, 12, 28), 7); // true
    BirthdayImminent(new DateTime(1980, 2, 28), new DateTime(2012, 2, 21), 7); // true

    private static bool BirthdayImminent(DateTime birthDate, DateTime referenceDate, int days)
    {
        DateTime birthdayThisYear = birthDate.AddYears(referenceDate.Year - birthDate.Year);

        if (birthdayThisYear < referenceDate)
            birthdayThisYear = birthdayThisYear.AddYears(1);

        bool birthdayImminent = (birthdayThisYear - referenceDate).TotalDays <= days;

        return birthdayImminent;
    }

Also keep the edge case in mind Guvante posted in the comments below.

like image 162
Caramiriel Avatar answered Sep 20 '22 16:09

Caramiriel


You could use "DayOfYear":

public bool IsBirthdayImminent
{
    get { return DateOfBirth != null && Math.Abs(DateOfBirth.Value.Date.DayOfYear - DateTime.Today.DayOfYear) <= 7; }
}
like image 44
bbar Avatar answered Sep 21 '22 16:09

bbar