ExtensionMethod.NET Home of 818 C#, Visual Basic, F# and Javascript extension methods

IsBetween

Checks if the date is between the two provided dates

Source

public static Boolean IsBetween(this DateTime dt, DateTime startDate, DateTime endDate, Boolean compareTime = false)
{
   return compareTime ?
      dt >= startDate && dt <= endDate :
      dt.Date >= startDate.Date && dt.Date <= endDate.Date;
}

Example

var today = DateTime.Now;
var start = new DateTime(2012, 1, 1);
var end = new DateTime(2013, 11, 25);

Boolean isBetween = today .IsBetween(start, end);

Author: hybrid2102

Submitted on: 25 nov 2013

Language: C#

Type: System.DateTime

Views: 6156