Compare DateTime without a year

I try to get a warning when the Client has his birthday in the next 7 days.

I tried this with this code:

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

Of course, this does not work, since the date is stored with its year (say, 05/21/1980), and also compares the year. Thus, this request will never be true - well, if you are born in the next seven days, though.

How can I modify this query to ignore the year?

Edit:

Well, the request itself is not a problem at all. My highlight is handling leap years and situations in December - January.

+7
source share
5 answers

I would suggest using the following code. This includes cases from December to January and February 29. Although you can take a look and fix February 28th to include or exclude during 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 check out the Guvante framework in the comments below.

+8
source

Something like that:

 DateTime birthDate = new DateTime(2012, 12, 2); DateTime birthdayThisYear; if (birthDate.Month == 2 && birthDate.Day == 29 && DateTime.IsLeapYear(DateTime.Now.Year)) birthdayThisYear = new DateTime(DateTime.Now.Year, 2, 28); else birthdayThisYear = new DateTime(DateTime.Now.Year, birthDate.Month, birthDate.Day); bool birthdayImminent = birthdayThisYear > DateTime.Now && (birthdayThisYear - DateTime.Now).TotalDays <= 7; 

As a recipient:

 public bool IsBirthdayImminent { get { if (DateOfBirth == null) return false; else { DateTime birthdayThisYear; if (birthDate.Month == 2 && birthDate.Day == 29 && DateTime.IsLeapYear(DateTime.Now.Year)) birthdayThisYear = new DateTime(DateTime.Now.Year, 2, 28); else birthdayThisYear = new DateTime(DateTime.Now.Year, birthDate.Month, birthDate.Day); return birthdayThisYear > DateTime.Now && (birthdayThisYear - DateTime.Now).TotalDays <= 7; } } } 
+1
source

Set the birtdate date explicitly to DateTime.Today.Year and it only compares the penalty.

0
source

You can use "DayOfYear":

 public bool IsBirthdayImminent { get { return DateOfBirth != null && Math.Abs(DateOfBirth.Value.Date.DayOfYear - DateTime.Today.DayOfYear) <= 7; } } 
0
source

Try the following:

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

All Articles