Date Comparison Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Haven't tested this, so be warned...

private bool IsBetween(DateTime startDate, DateTime endDate, DateTime date)
{
TimeSpan startSpan = startDate - date;
if(startSpan.Days > 0) { // Dunno if you want to use days or
something finer grained
// Also not sure if you want
inclusive or not
TimeSpan endSpan = endDate - date;
if(endSpan.Days > 0) {
return true;
}
}

return false;

}
 
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
 
MarkAurit said:
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);

if (dateToTest >= minDate && dateToTest <= maxDate)
{
....
}
 
Jon Skeet said:
if (dateToTest >= minDate && dateToTest <= maxDate)
{
...
}

Ohhhh bugger! There I was happily playing around with op_Subtract and
TimeSpans and I completely forgot that DateTime supported the comparison
operators...
 
Thanks, Jon.
This fixes that issues of comparison. However, my problem is more subtle:
even if the date is the same, the test isnt correctly working because the
time isnt the same. Im taking a date which comes from a business object
where its defined as a string - even if its todays date, when I perform a
DateTime.Parse on it and equality comparing it to DateTime.Now, the test is
false.
So: I need to compare only the date part of a DateTime, and I have to test
for greater than (if its was always equality, I could ToDateString()).
Any help is hugely appreciated.
 
MarkAurit said:
This fixes that issues of comparison. However, my problem is more subtle:
even if the date is the same, the test isnt correctly working because the
time isnt the same. Im taking a date which comes from a business object
where its defined as a string - even if its todays date, when I perform a
DateTime.Parse on it and equality comparing it to DateTime.Now, the test is
false.
So: I need to compare only the date part of a DateTime, and I have to test
for greater than (if its was always equality, I could ToDateString()).
Any help is hugely appreciated.

Just use the Date property of DateTime to get a DateTime with an empty
time portion. You can use DateTime.Today to get the equivalent of
DateTime.Now.Date.
 
Back
Top