I need some clear mind to check if a date range false between another interval. Here is whats happening:
DateTime[] dates = new DateTime[20];
dates[0] = Convert.ToDateTime(initial_date);
for (int i = 1; i <= 19; i++)
{
dates[i] = initial_date.AddYears(i);
}
So i have an array that stores 20 dates. If the initial_date = 1/20/2012 the array goes from dates[0] = 1/20/2012 up to dates[19] = 1/20/2031
and now i want to check if a user selects two dates for example 1/1/2013 and 1/1/2014 the selections falls between the the first element of the array (dates[0]) and the second (dates[1]). so far:
DateTime a1 = Convert.ToDateTime(vtable.Rows[0][0]);
DateTime a2 = Convert.ToDateTime(vtable.Rows[vtable.Rows.Count - 1][0]);
DateTime start = DateTime.MinValue;
DateTime end = DateTime.MaxValue;
for (int i = 0; i < 20; i++)
{
if (a1.CompareTo(dates[i]) >= 0)
{
start = dates[i];
for (int j = 19; j > 0 ; j--)
{
if (a2.CompareTo(dates[j]) >= 0)
{
end = dates[j];
break;
}
}
break;
}
}
this is working up to the point when a user selects a daterange that falls only between one element of the array. Like for example if the selection is 1/30/2012 – 5/30/2012 then start = date[0] and end = date[0]
I know i can simply state at the end if end == unassigned then end = start but i am thinking is better to correct the algorithm than applying a patch at the end
Thank you very much
Store your date ranges as pairs as in start to end, where end would next start – 1 day.
Makes things much clearer in the code.