Возможно, вам понадобится вспомогательная функция, чтобы получить правильное значение и для лепестков - тех, кто родился 29 февраля. Хитрость заключается в использовании AddYears
, который не подлежит високосным годам.
Он может быть создан как метод растяжения DateTime
:
/// <summary>
/// Calculates the next annual day of this instance of System.DateTime relative to today.
/// <para>Calculates correctly Feb. 28th as an annual day in common years for event dates of Feb. 29th.</para>
/// <para>If next annual day should be later than 9999-12-31, the annual day of year 9999 is returned.</para>
/// </summary>
/// <param name="eventDate">The date of the event.</param>
/// <returns>The date of the upcoming annual day.</returns>
public static DateTime NextAnnualDay(this DateTime eventDate)
{
return NextAnnualDay(eventDate, DateTime.Today);
}
/// <summary>
/// Calculates the next annual day of this instance of System.DateTime relative to a future date.
/// <para>Calculates correctly Feb. 28th as an annual day in common years for event dates of Feb. 29th.</para>
/// <para>If futureDate is earlier than this instance, the value of this instance is returned.</para>
/// <para>If next annual day should be later than 9999-12-31, the annual day of year 9999 is returned.</para>
/// </summary>
/// <param name="eventDate">The date of the event.</param>
/// <param name="futureDate">The date from which to find the next annual day of this instance.</param>
/// <returns>The date of the upcoming annual day.</returns>
public static DateTime NextAnnualDay(this DateTime eventDate, DateTime futureDate)
{
DateTime nextDate = eventDate;
if (DateTime.MaxValue.Year - eventDate.Year == 0)
{
// No later next annual day can be calculated.
}
else
{
int years = futureDate.Year - eventDate.Year;
if (years < 0)
{
// Don't calculate a hypothetical next annual day.
}
else
{
nextDate = eventDate.AddYears(years);
if (nextDate.Subtract(futureDate).Days <= 0)
{
if (DateTime.MaxValue.Year - nextDate.Year == 0)
{
// No later next annual day can be calculated.
}
else
{
nextDate = eventDate.AddYears(years + 1);
}
}
}
}
return nextDate;
}
Затем идут так (псевдо SQL):
select name, category, dob
from family_details
where dob.NextAnnualDay() between today and today.AddDays(15)
order by dob desc
Finish с помощью Linq или преобразовать в параметризированного SQL.
[SO Post] (http://stackoverflow.com/questions/5459287/find-the-next-month-for-given-date-in-mysql) думает, что он отвечает на ваш вопрос –
Возможный дубликат [SQL Select Предстоящие дни рождения] (http://stackoverflow.com/questions/83531/sql-select-upcoming-birthdays) – Filburt
Он не работает ... –