If else of date

-1

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
}
    
asked by anonymous 01.11.2016 / 05:09

1 answer

5

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.

    
01.11.2016 / 11:27