如何确定生日或周年纪念日发生在日期范围内 [英] How to determine if birthday or anniversary occurred during date range

查看:85
本文介绍了如何确定生日或周年纪念日发生在日期范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我有一个生日/周年纪念日DateTime,如何确定该日期是否在特定日期范围内发生?例如,

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

生日= 1/2/2000

日期范围= 12/25/2008-1/3/2009

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

我需要一种方法来确定此人的生日是否在该日期范围内发生-最好是在C#中。

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

我首先更改了生日DateTime的年份以匹配日期范围,然后只需检查新生日DateTime是否在日期范围的开始和结束日期之间...但是当日期范围跨越不同的年份时,例如我上面的示例-我不得不添加一个讨厌的if语句。没有更好的方法吗?

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;
}

这篇关于如何确定生日或周年纪念日发生在日期范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆