If Else of date

Asked

Viewed 82 times

-1

I have a viewbag, and wanted to display dates of at most 1 day before the current day, I did the code below, but without success, which alternative I would have?

if (item.DataHora < DateTime.Now - 1)
{
//codigo
}
  • How about item.DataHora > DateTime.Today.Subtract(TimeSpan.FromDays(1))?

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...

  • thanks @Randrade, gave it right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.