鉴于我有一个生日/纪念日 DateTime,我如何确定该日期是否发生在特定日期范围内?例如,
生日 = 1/2/2000
日期范围 = 12/25/2008 - 1/3/2009
我需要一种方法来确定此人的生日是否发生在该日期范围内 - 最好是在 C# 中。
我首先着手更改生日 DateTime 的年份以匹配日期范围,然后只检查"new"生日 DateTime 是否在日期范围的开始日期和结束日期之间...但是当日期范围跨越不同的年份,就像我上面的例子一样——我不得不添加一个讨厌的 if 语句。有没有更好的办法?
最佳答案
好的,这是我的看法
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;
}
关于c# - 如何确定生日或周年纪念日是否发生在日期范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2553663/