Skip to main content

Validate Between Two Times - C#

How to check if DateTime.Now is between two given DateTimes for time part only?


Since you are only gathering two times without dates, you need to figure out if the two times are from the same day or not. If you put the StartTime, EndTime, and Now into TimeSpans:
If (StartTime > EndTime) 
{
    // the range crosses midnight, do the comparisons independently
    Return (StartTime < Now) || (Now < EndTime);
}
Else 
{
    // the range is on the same day, both comparisons must be true
    Return StartTime < Now && Now < EndTime;
}















Ref : http://stackoverflow.com/questions/12998739/how-to-check-if-datetime-now-is-between-two-given-datetimes-for-time-part-only







Comments