-1

it is possible with C # test whether the DateTime " 01/29/2016 16:22:00 " is included in the next 24 hours? I have a date that is " 01/28/2016 15:30 " ( DateTime.Now ) and I 'd like to know if it is within the range going forward 24 hours. I hope I explained .

Mr. Developer
  • 3,061
  • 7
  • 38
  • 96

2 Answers2

4
(yourDatetime - DateTime.Now) <= TimeSpan.FromHours(24)
M.kazem Akhgary
  • 17,601
  • 7
  • 53
  • 108
3

You can use AddHours:

var myDateTime = ...
var now = DateTime.Now;

if (myDateTime <= now.AddHours(24)
    && myDateTime >= now.AddHours(-24))
{

}

Also keep in mind that DateTime is immutable and they cannot be modified after creation. That is why AddHours returns a new instance.

Update: After seeing M.kazem's answer I have though that you can also use that:

Math.Abs(myDateTime.Subtract(DateTime.Now).TotalHours) <= 24
Farhad Jabiyev
  • 24,866
  • 7
  • 63
  • 96