There are two dates of the type System.DateTime what (most correct) method can you check whether the third date of the type System.DateTime in the interval between them?
1 answer
public static class DateTimeExtensions { public static bool IsInRange(this DateTime dateToCheck, DateTime startDate, DateTime endDate) { return dateToCheck >= startDate && dateToCheck < endDate; } } Using:
DateTime startDate = DateTime.Now.AddDays(-1); DateTime endDate = DateTime.Now.AddDays(1); DateTime dateToCheck = DateTime.Now; dateToCheck.IsInRange(startDate, endDate) Before comparing DateTime objects, make sure they represent the time in the same time zone. ( more info )
- I didn’t think that dates can be compared like this ... - Dmitry Nail
- And why only less than the upper range, and not less or equal? - Dmitriy Nail
- 2whether or not to include the upper range is up to you. still such a moment, the dates should be in the same time zone - Ruslan_K
|