I have a viewbag, and wanted to display dates of at most 1 day before the current day, I made the code below, but without success, what alternative would I have?
if (item.DataHora < DateTime.Now - 1)
{
//codigo
}
I have a viewbag, and wanted to display dates of at most 1 day before the current day, I made the code below, but without success, what alternative would I have?
if (item.DataHora < DateTime.Now - 1)
{
//codigo
}
You can use the method .AddDays () . With it you can pass a positive value to add days, or a negative value to subtract.
if (item.DataHora > DateTime.Now.AddDays(-1))
{
//codigo
}
See a example in .netFiddle.
Remembering that there are also variations for days, months, years, seconds, etc.