如何确定是否生日或纪念日的日期范围内发生 [英] How to determine if birthday or anniversary occured during date range

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

问题描述

由于我有一个生日/周年日期时间,我怎么能确定一个特定的日期范围内发生的日期?例如,

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

生日= 2000年1月2日结果
日期范围= 12/25/2008 - 2009/1/3

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#.

我第一次去有关更改生日日期时间的年份相匹配的日期范围,然后只检查是否新的生日日期时间的日期范围的开始和结束日期之间...但是,当日期范围跨越不同的年份,像我上面的例子 - 我不得不添加一个讨厌的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天全站免登陆