How to determine if birthday or anniversary occurred during date range

Given I have a birthday/anniversary DateTime, how can I determine if that date occurred during a specific date range? For example,

Birthday = 1/2/2000
Date Range = 12/25/2008 - 1/3/2009

I need a method to determine whether or not this person's birthday happened during that date range - preferably in C#.

I first went about changing the year of the birthday DateTime to match the date range, then just check if the "new" birthday DateTime is between the start and end date of the date range... but when the date range spans different years, like in my example above - I had to add a nasty if statement. Is there no better way?


好的,这是我的要求

public static bool IsBirthDayInRange(DateTime birthday, DateTime start, DateTime end)
{
    DateTime temp = birthday.AddYears(start.Year - birthday.Year);

    if (temp < start)
        temp = temp.AddYears(1);

    return birthday <= end && temp >= start && temp <= end;
}

I'm assuming that your dates are stored in DateTime variables? If so, the comparison is pretty straight forward:

if (Birthday > DateRangeLower && Birthday < DateRangeUpper) {
    // it's your birthday!
}

You can encapsulate this in an extension method if you like:

public static bool Between(this DateTime compareDate, DateTime startDate, DateTime endDate) {
    return compareDate > startDate && compareDate < endDate;
}

then you can call it like such:

if (Birthday.Between(DateRangeLower, DateRangeUpper) {
    // it's your birthday
}

Update : If you want to ignore the birthday's year part to determine if the anniversary of the date is within the range, apply the following:

if (DateRangeLower.DayOfYear <= DateRangeUpper.DayOfYear &&  
    Birthday.DayOfYear > DateRangeLower.DayOfYear && Birthday.DayOfYear < DateRangeUpper.DayOfYear) {
  // it's your birthday
  // the days are within the date range (and the range is in a single year)
}
else if (DateRangeLower.DayOfYear > DateRangeUpper.DayOfYear &&
    Birthday.DayOfYear < DateRangeLower.DayOfYear && Birthday.DayOfYear > DateRangeUpper.DayOfYear) {
  //  it's your birthday
  //  note, we're actually checking to see if the date is outside of the
  //  original date's days to handle the case where the dates span a year end boundary
  //  this only works if the dates are not more than 1 year apart
}

Updated answer to include the upper bound normalization mentioned by SLC. This should work for the cases where the person is nor born on 29/02.

DateTime birthday = new DateTime(2000, 2, 1);

DateTime min = new DateTime(2008, 12, 25);
DateTime max = new DateTime(2009, 3, 1);

DateTime nLower = new DateTime(min.Year, birthday.Month, birthday.Day);
DateTime nUpper = new DateTime(max.Year, birthday.Month, birthday.Day);

if (birthday.Year <= max.Year && 
    ((nLower >= min && nLower <= max) || (nUpper >= min && nUpper <= max)))
{
    // Happy birthday
    Console.WriteLine("Happy birthday");
}

And now a version that handles people born on the day (29/02):

public static bool IsBirthdayInRange(
    DateTime birthday, DateTime min, DateTime max)
{
    var dates = new  DateTime[] { birthday, min };
    for (int i = 0; i < dates.Length; i++)
    {
        if (dates[i].Month == 2 && dates[i].Day == 29)
        {
            dates[i] = dates[i].AddDays(-1);
        }
    }

    birthday = dates[0];
    min = dates[1];

    DateTime nLower = new DateTime(min.Year, birthday.Month, birthday.Day);
    DateTime nUpper = new DateTime(max.Year, birthday.Month, birthday.Day);

    if (birthday.Year <= max.Year &&
        ((nLower >= min && nLower <= max) || (nUpper >= min && nUpper <= max)))
    {
        return true;
    }

    return false;
}
链接地址: http://www.djcxy.com/p/54406.html

上一篇: 日期范围与可空日期重叠

下一篇: 如何确定日期范围内是否有生日或周年纪念