-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,295
  • 7
  • 43
  • 110

2 Answers2

6
(yourDatetime - DateTime.Now) <= TimeSpan.FromHours(24)
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
4

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
  • 26,014
  • 8
  • 72
  • 98