简体   繁体   English

C#如何确定日期是否在180天之内

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

How can I determine if a date is within 180 days? 如何确定日期是否在180天内?

I've been playing with DateTime.Compare below and using date1 +180 days (date1 is 28th August 2009 13:12) 我一直在玩DateTime.Compare,使用date1 +180天(date1是2009年8月28日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. 这将告诉您date1是否在180天内在现在的方向 - 过去或将来。 If you only need into the past, go with Developer Art 's answer . 如果您只需要过去,请使用Developer Art答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM