private void button16_Click(object sender, System.EventArgs e)
{
DateTime givenDate = DateTime.Now;
DateTime startDate = DateTime.Now.Subtract(TimeSpan.FromDays(5));
DateTime endDate = DateTime.Now;
// Test a date inside the range.
if ( Utils.DateBetween(startDate, endDate, givenDate) )
Console.WriteLine("Date is between");
else
Console.WriteLine("Date is not between");
// Test with a date outside the range.
givenDate = startDate.Subtract(TimeSpan.FromSeconds(1));
if ( Utils.DateBetween(startDate, endDate, givenDate) )
Console.WriteLine("Date is between");
else
Console.WriteLine("Date is not between");
}
/// <summary>
/// Returns true if date is between start and end date inclusive. Put in
some static class, etc.
/// </summary>
public static bool DateBetween(DateTime start, DateTime end, DateTime date)
{
if ( date >= start && date <= end )
return true;
return false;
}
// Output
Date is between
Date is not between
--
William Stacey, MVP
http://mvp.support.microsoft.com
"MarkAurit" <Ma*******@discussions.microsoft.com> wrote in message
news:C3**********************************@microsof t.com...
Im having difficulty coming up with a good algorithm to express the
following comparison:
"if <a given date> falls between the (current date - 5 days) and the
(current date)"
Obviously. DateTime.Now and something like (AddDays(DateTime.Now,-5) are
used for the inner and outer ranges, its how to express the "between" that has
me.
/* what Id like to do, in pseudo code */
dateToTest=DateTime.Parse("mm/dd/yy");
maxDate=DateTime.Now;
minDate=DateTime.Now.AddDays(-5);
bool b1=IsBetween(minDate,maxDate,dateToTest);
Thanks, Mark