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?
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.
DateTime d1;
DateTime d2;
if((d1 - d2).TotalDays == 10)
{
//some code
}
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.
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
if (DateTime.Now.Date> dt.Date.AddDays(10).Date)
{
e.Item.Style.Add("background-color", "#C400F9");
break;
}
if (DateTime.Now.Date > dt.Date.AddDays(10))
{
}
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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.