I’m faced with an issue where my application is doing a search for transactions based on a FROM and TO date.
Lets use the following example:
TRANS1: 14-Feb-2012 2:23:36
TRANS2: 07-Feb-2012 3:23:47
My date ranges in C# is as follow:
startDate.Date {7/02/2012 12:00:00 AM}
endDate.Date {14/02/2012 12:00:00 AM}
The following line of code will ALWAYS exclude transactions if they fall on the endDate because the endDate is always set to 12:00:00AM (Based on DateTime.Now)
if (trans.TransactionDate >= startDate.Date &&
trans.TransactionDate <= endDate.Date)
{
// do stuff
}
How do I correctly handle this so that it includes all transactions for the 14th as well? Is it safe to do the following:
Changing 12:00:00AM to 12:00:00PM
if (trans.TransactionDate >= startDate.Date &&
trans.TransactionDate <= endDate.Date.AddHours(12))
{
// do stuff
}
This may work: