Check if datetime instance falls in between other two datetime objects Check if datetime instance falls in between other two datetime objects asp.net asp.net

Check if datetime instance falls in between other two datetime objects


DateTime.Ticks will account for the time. Use .Ticks on the DateTime to convert your dates into longs. Then just use a simple if stmt to see if your target date falls between.

// Assuming you know d2 > d1if (targetDt.Ticks > d1.Ticks && targetDt.Ticks < d2.Ticks){    // targetDt is in between d1 and d2}  


Do simple compare > and <.

if (dateA>dateB && dateA<dateC)    //do something

If you care only on time:

if (dateA.TimeOfDay>dateB.TimeOfDay && dateA.TimeOfDay<dateC.TimeOfDay)    //do something


Write yourself a Helper function:

public static bool IsBewteenTwoDates(this DateTime dt, DateTime start, DateTime end){    return dt >= start && dt <= end;}

Then call: .IsBewteenTwoDates(DateTime.Today ,new DateTime(,,));