0
votes

tl;dr; How would you take StartDate-DateTime: Dec 10th 2011, EndDate-DateTime Jan 15th 2012, and determine whether or not Dec 1, Dec 9,Dec 17,Dec 25, Jan 1, and Jan 9 fit into that TimeSpan, Year excluded and get a Bool for each one?

I have a visual timespan consisting of a linear graph showing when a specific activity is active.

enter image description here

I am given an object with a start date, and an end date.

I have covered the timespan with a DIV having a red background representing 1/4 of each month, naming them JanQ1, JanQ2, etc..

These start off as visibility:hidden, but need to be altered in the event that the activity is active during the part of the year.

The problem I'm having is getting a true/false value that ignores the year being given.

For example, an event that goes from Dec 10th 2011 to Jan 15th 2012, I would want this result set:

DecQ1=False,
DecQ2=True,
DecQ3=True,
DecQ4=True,
JanQ1=True,
JanQ2=True,
JanQ3=False
2
Is it normal that StartDate > EndDate?vc 74
If year isn't important, just initialize them all with 0001 for example (i.e. use the year from DateTime.MinValue). However, as vc 74 is hinting at, you need to really know that Jan (next year) is after Dec (this year) for example.dash
StartDate will always happen before EndDate. The problem with homogenizing the year is, when StartDate and EndDate start with different years, but end up in the same year, true becomes false with the date range (I.E. the >/< compare becomes between jan and dec, instead of between dec and january)Wesley

2 Answers

1
votes

Would a function like this work for you?

bool IsDateContained(DateTime startDate, DateTime endDate, int month, int day) {
    bool retVal = false;
    if (startDate < endDate) {
        do {
            if (startDate.Month == month && startDate.Day == day) {
                retVal = true;
                break;
            }
            startDate = startDate.AddDays(1);
        } while (startDate < endDate);
    } else {
        //crazy world!!
    }
    return retVal;
}
0
votes

Make new DateTime objects before comparison by setting them to the same year - http://msdn.microsoft.com/en-us/library/wb249tb7.aspx.