user123456789 user123456789 - 15 days ago 4
ASP.NET (C#) Question

check if number of days has passed since a date

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?

Answer

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.