Here is my solution. It uses DayOfYear
to find a match. But you have to take care, if the DayOfYear
of the start-date is past the DayOfYear
of the end-date. I assume, that the start date is earlier than the end date:
private static bool HasBirthDay( DateTime birthday, DateTime start, DateTime end )
{
Debug.Assert( start < end );
if( start.DayOfYear < end.DayOfYear )
{
if( birthday.DayOfYear > start.DayOfYear && birthday.DayOfYear < end.DayOfYear )
{
return true;
}
}
else
{
if( birthday.DayOfYear < end.DayOfYear || birthday.DayOfYear > start.DayOfYear )
{
return true;
}
}
return false;
}
// DayOfYear(start date) > DayOfYear(end date)
var start = new DateTime( 2008, 12, 25 );
var end = new DateTime( 2009, 1, 3 );
Debug.Assert( HasBirthDay( new DateTime( 2000, 1, 2 ), start, end ) );
Debug.Assert( HasBirthDay( new DateTime( 2000, 12, 26), start, end ) );
Debug.Assert( !HasBirthDay( new DateTime( 2000, 1, 5 ), start, end ) );
Debug.Assert( !HasBirthDay( new DateTime( 2000, 12, 24 ), start, end ) );
// DayOfYear(start date) < DayOfYear(end date)
start = new DateTime( 2008, 10, 25 );
end = new DateTime( 2008, 11, 3 );
Debug.Assert( HasBirthDay( new DateTime( 2000, 10, 26 ), start, end ) );
Debug.Assert( !HasBirthDay( new DateTime( 2000, 12, 5 ), start, end ) );
Debug.Assert( !HasBirthDay( new DateTime( 2000, 1, 24 ), start, end ) );