How to know if a DateTime is between a DateRange in C#

Nope, doing a simple comparison looks good to me:

return dateToCheck >= startDate && dateToCheck < endDate;

Things to think about though:

  • DateTime is a somewhat odd type in terms of time zones. It could be UTC, it could be “local”, it could be ambiguous. Make sure you’re comparing apples with apples, as it were.
  • Consider whether your start and end points should be inclusive or exclusive. I’ve made the code above treat it as an inclusive lower bound and an exclusive upper bound.

Leave a Comment