i have 2 dates : date1 and date2 ; I want to check is that another date is between date1 and date2 thanks very much
Asked
Active
Viewed 122 times
2
-
Re: The current answers, unless you know the order of `date1` and `date2`, you'll want to sort that out before comparing it to `anotherDate`'. – HABO May 06 '12 at 13:52
-
duplicate of: http://stackoverflow.com/questions/5672862/check-if-datetime-instance-falls-in-between-other-two-datetime-objects – Adam May 06 '12 at 14:13
2 Answers
6
You can just use the standard <, >, >= and <= operators:
if( someDate >= date1 && someDate <= date2 )
{
}
And, you can make your own extension method for it:
public static class DateExtensions
{
public static bool Between( this DateTime d, DateTime start, DateTime end )
{
return d >= start && d <= end;
}
}
Which you can use like this:
DateTime someDate = new DateTime (2012, 5, 6);
if( someDate.Between (date1, date2) )
{
...
}

Frederik Gheysels
- 56,135
- 11
- 101
- 154
3
That's simple:
if (date3 >= date1 && date3 <= date2)

McGarnagle
- 101,349
- 31
- 229
- 260

Steve
- 213,761
- 22
- 232
- 286