简体   繁体   English

检查日期是否在日期范围内

[英]Checking if date is within a date range

I have a GridView and I want to change the colour of the row if the date in a column is within a certain date range. 我有一个GridView,并且如果列中的日期在某个日期范围内,我想更改行的颜色。

DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
    if (dt.Date >= DateTime.Now.AddDays(Mod.ValidUntilDays).Date && dt.Date <= DateTime.Now.AddDays(Mod.ValidUntilDays).Date)
     {
         e.Item.Style.Add("background-color", "#FF0303");
     }
}

So the value coming from the grid is dt.Date is equal to 23/09/2016 00:00:00 and the date range I want to check is 25/09/2016 00:00:00 . 所以来自网格的值是dt.Date等于23/09/2016 00:00:00 ,我要检查的日期范围是25/09/2016 00:00:00 Mod.ValidUntilDays just adds 5 days to Today's date. Mod.ValidUntilDays仅将5天添加到今天的日期。

So what I am trying to do is check is 23/09/2016 00:00:00 within the 5 day range which it is but the code never goes into the if statement. 所以我想做的是检查是在5天范围内的23/09/2016 00:00:00 ,但是代码永远不会进入if语句。

you are asking the date to be >= 5-days-time and <= 5-days-time. 您要求日期为> = 5天时间和<= 5天时间。 So unless it == 5-days-time it'll return false. 因此,除非它== 5天时间,否则它将返回false。 I think you mean this: 我想你的意思是:

DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
  DateTime now = DateTime.Now;
  if (dt.Date >= now.Date && dt.Date <= now.AddDays(Mod.ValidUntilDays).Date)
  {
     e.Item.Style.Add("background-color", "#FF0303");
  }
}

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

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