C# 比较没有年份的日期时间

C# 比较没有年份的日期时间,c#,datetime,C#,Datetime,我试图在客户在未来7天内过生日时得到提醒 我用以下代码尝试过: public bool IsBirthdayImminent { get { return DateOfBirth != null && DateOfBirth.Value.Date >= DateTime.Today.Date.AddDays(-7); } } 当然,这不起作用,因为日期与年份(比如说1980年5月21日)一起存储,并且还比较年份。因此,这个查询永远不会是真的——好吧,如果你在接下来

我试图在客户在未来7天内过生日时得到提醒

我用以下代码尝试过:

public bool IsBirthdayImminent
{
    get { return DateOfBirth != null && DateOfBirth.Value.Date >= DateTime.Today.Date.AddDays(-7); }
}
当然,这不起作用,因为日期与年份(比如说1980年5月21日)一起存储,并且还比较年份。因此,这个查询永远不会是
真的
——好吧,如果你在接下来的七天内出生的话就不会了

如何修改此查询以忽略年份

编辑:


好的,查询本身根本不是问题。我的主要观点是处理闰年和12-1月前后的情况。

将鸟的年份明确设置为
DateTime。今天。年份将比较好。

类似于这样:

DateTime birthDate = new DateTime(2012, 12, 2);

DateTime birthdayThisYear;
if (birthDate.Month == 2 && birthDate.Day == 29 && DateTime.IsLeapYear(DateTime.Now.Year))
    birthdayThisYear = new DateTime(DateTime.Now.Year, 2, 28);
else
    birthdayThisYear = new DateTime(DateTime.Now.Year, birthDate.Month, birthDate.Day);

bool birthdayImminent = birthdayThisYear > DateTime.Now && (birthdayThisYear - DateTime.Now).TotalDays <= 7;
DateTime birthDate=新日期时间(2012年12月2日);
今年的生日日期;
if(birthDate.Month==2&&birthDate.Day==29&&DateTime.IsLeapYear(DateTime.Now.Year))
birthdayThisYear=新日期时间(DateTime.Now.Year,2,28);
其他的
birthdayThisYear=新日期时间(DateTime.Now.Year,birthDate.Month,birthDate.Day);
bool birthdayappoint=birthdaythisnear>DateTime.Now&(birthdaythisnear-DateTime.Now)。TotalDays DateTime.Now&(birthdaythisnear-DateTime.Now)。TotalDays试试以下方法:

public bool IsBirthdayImminent
{
    get { return DateOfBirth != null && DateOfBirth.Value.Date.AddYear(DateTime.Now.Year -DateOfBirth.Value.Year) >= DateTime.Today.Date.AddDays(-7); }
}
您可以使用“DayOfYear”:

public bool isbirthday即将到来
{

get{return DateOfBirth!=null&&Math.Abs(DateOfBirth.Value.Date.DayOfYear-DateTime.Today.DayOfYear)我建议使用以下代码。这包括12月-1月和2月29日左右的案例。尽管您可能希望查看并更正2月28日在给定的
天内被包括或排除

    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;
    }
birthdayimporting(新日期时间(1980,1,1),新日期时间(2012,1,2,7);//false
生日即将到来(新日期时间(1980,1,1),新日期时间(2012,12,28,7);//正确
生日即将到来(新日期时间(1980,2,28),新日期时间(2012,2,21),7);//正确
私有静态bool birthday即将到来(DateTime birthDate、DateTime referenceDate、int days)
{
DateTime birthdayThisYear=birthDate.AddYears(referenceDate.Year-birthDate.Year);
如果(今年的生日<参考日期)
birthdayThisYear=birthdayThisYear.AddYears(1);


bool birthdayappoint=(birthdaythisnear-referenceDate)。TotalDays Try:DateOfBirth.Value.Date>=DateTime.Today.Date.AddYears(DateOfBirth.Value.Date.Year-DateTime.Today.Date.Year)。AddDays(-7)使两个日期正常化:复制一份DOB值,并将年份设置为与当前年份相同。现在,您将对每个年份进行比较。@Idle_Mind这可能与闰年没有问题?例如,如果其中一个日期是1996年2月29日,而您试图将其设置为2013年2月29日,则可能无法获得所需的结果。其他任何日期也一样呃,不同年份的日期之间的差异。很好的一点@Servy.Hmmm…@SeToY,你检查过我的吗?但是要注意12月1日左右的情况。这是如何处理闰年的?如果你的生日在闰年,并且在2月29日之后,一年中的某一天比非闰年的那一天多一天,不?嗯,很好的一点。DayOfYear只是返回一个值整数值介于1和366之间,所以您是对的;这会干扰您的结果。@Servy如果在
DateofBirth.value.Date
DateTime.Today
之间有一个2月29日,则此减法的值将比它应该的值小1。如果有两个或更多,它将在1天内关闭。@JeremyPridemore如果当前日期在2月底之后,并且出生年份是闰年,或者当前年份是闰年,则会出现问题。如果两者都是闰年,或者两者都不是,则可以。它总是正确的,或者在一天之前关闭。这也不考虑12月底/1月初的生日,因此回顾一下这是一个垃圾答案;我稍后会删除它。如果生日是在过去7天内,或者是从现在到年底之间的任何时间,这将返回真的。这不是很优雅,但有更好的解决方案!我同意@Jim-1如果某人出生在2月29日,这将抛出一个例外,大多数年份你称之为它。@SeToY-看起来不错。它是也值得添加一个检查,以确保bithdate在未来,即,&&birthdayThisYear>DateTime。现在@Servy-Good spot需要检查月份为'Feb',日期为'29th'的生日,OP需要决定如何处理-1天。应该是在当前年份不是闰年的情况下。此外,我认为还有除了2月28日之外,在某些文化中,在某些时间还有其他闰日,因此我不完全确定这是否适用于所有情况。它也不适用于12月至1月的生日,但当前日期是12月底。
AddYears
的+1,而不是试图拼凑一个构造函数。从技术上来说
days>=60
您对闰年生日的处理被关闭了一次。使这两个
AddYears
功能都关闭
birthDate
将修复该边缘条件(
2012年2月29日。AddYears(3)。AddYears(1)==2016年2月28日
).但是考虑到要求,我认为你的方法是最好的,因为修正增加了非明显合理的冗余。@Guvante这不适用于所有大于等于60的日子,只适用于闰年,而当前日期也是闰年,但在28日之前。但在这种情况下,它会被关闭一天。这就是为什么我很少回答DateTimestions。它们非常凌乱。@Servy:你认为纠正今年
生日(上次作业后)和
生日之间的闰日数就足够了吗?@Caramiriel事实上,回顾过去,这从来都不是问题。正如Guv所示,如果你两次调用addyears(比如说加上3年,然后再加上一年)这并不总是等同于一次性加上4年。但是,你唯一能加上年的时间是在一年中生日早于生日的时候
public bool IsBirthdayImminent
{
    get { return DateOfBirth != null && Math.Abs(DateOfBirth.Value.Date.DayOfYear - DateTime.Today.DayOfYear) <= 7; }
}
    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;
    }