开发者

C# How to determine if a date is within 180 days of now

开发者 https://www.devze.com 2022-12-20 20:51 出处:网络
How can I determine if a 开发者_StackOverflowdate is within 180 days? I\'ve been playing with DateTime.Compare below and using date1 +180 days (date1 is 28th August 2009 13:12)

How can I determine if a 开发者_StackOverflowdate is within 180 days?

I've been playing with DateTime.Compare below and using date1 +180 days (date1 is 28th August 2009 13:12)

DateTime.Compare(**date1**.AddDays(180), now) 

Is this correct?

Thanks

Jamie


DateTime.Now.Subtract(dt1).Days <= 180


if (Math.Abs(DateTime.Now.Subtract(date1).Days) <= 180)
{
    ...
}

This will let you know if date1 is within 180 days IN EITHER DIRECTION of right now - past or future. If you only need into the past, go with Developer Art's answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消