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.
Caramiriel
source share