-1

I am trying to check if a certain number of days has passed since a date and if it has then change the color of the grid row. So if the date is 12/11/2016 and I want to check if 10 days has passed since that date.

if (dt.Date > dt.Date.AddDays(10))
{
     e.Item.Style.Add("background-color", "#C400F9");
     break;
}

So adding 10 days to the 12th would be the 22/11/2016 and since today is the 23/11/2016 that means 10 days has passed. But all rows in the grid are changing to the color. Do I need to add another if statement to compare the date + the days passed to today's date?

user123456789
  • 1,914
  • 7
  • 44
  • 100
  • Have you posted relevant example? Condition `dt.Date > dt.Date.AddDays(10)` will never be met, so yor rows can't change color based on this condition. It's hard to say what's wrong in your *real* condition.... – Andrey Korneyev Nov 23 '16 at 12:53
  • everyone mentioned the comparison issue, and I'm assuming that was just sample code because no rows will get the color and you are saying every row is getting it. Is `dt` a global date, or the date on the row? – bixarrio Nov 23 '16 at 12:56
  • Possible duplicate of [How can relative time be calculated in C#?](http://stackoverflow.com/questions/11/how-can-relative-time-be-calculated-in-c) – VDWWD Nov 23 '16 at 14:00
  • @VDWWD Not at all related – Rob Nov 24 '16 at 07:38

7 Answers7

7

Did you mean to use today's date? If so, that's DateTime.Now:

if (DateTime.Now > dt.Date.AddDays(10))

At the moment you are comparing dt.Date against same date plus 10 days - as others noted, this will never be met.

Update. As Tim suggests in comments, using DateTime.Today might be more appropriate.

Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Better use `DateTime.Today` instead of `DateTime.Now` here, otherwise midnight will return false but one second later you get true, both are same-day – Tim Schmelter Nov 23 '16 at 13:05
2
DateTime d1;
DateTime d2;

if((d1 - d2).TotalDays == 10)
{
  //some code
}
Timon Post
  • 2,779
  • 1
  • 17
  • 32
0

Your problem is in your comparison. You're trying to figure out if dt is bigger than dt + 10. Obviously it will never be true.

Augure
  • 360
  • 3
  • 13
0

Your comparison will never be true, it's like doing:

If (10 > 10 + 10)...

What you want to do is compare 10 days after the date with today using DateTime.Now

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
0
if (DateTime.Now.Date> dt.Date.AddDays(10).Date)
{
    e.Item.Style.Add("background-color", "#C400F9");
    break;
}
Danh
  • 5,916
  • 7
  • 30
  • 45
0
if (DateTime.Now.Date > dt.Date.AddDays(10))
{

}
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111
0

Can you Try this, Below X is date that you want to compare with(it may come form db or hardcoded date)

if (X > dt.Date.AddDays(10))
        {
            e.Row.Attributes.Add("background-color", "#C400F9");
        }
Vaibhav shetty
  • 372
  • 1
  • 4
  • 15
  • This is exactly the same code as the OP has in their question. Did you mean to edit it into something else? – ChrisF Nov 23 '16 at 13:11