views:

293

answers:

4

As the garbled question says I'm basically looking for a tidier way to do the following snip. (its used in a calendar for availability matching)

        //TODO: Optimize elseif Statement
        if (date.Year == now.Year && date.Month == now.Month && day == now.Day)
        {
            daysXhtml.Append("<td class=\"today\">" + day.ToString() + "</td>");
        }
        else if (((day == SelectedDate.Day)
               || (day != SelectedDate.Day && ((day == SelectedDate.AddDays(1).Day || (SelectedDate.Day > 3 && day == SelectedDate.AddDays(-1).Day)) && (day != SelectedDate.AddDays(2).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-2).Day) || day != SelectedDate.AddDays(3).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-3).Day))))
               || (day != SelectedDate.Day && ((day == SelectedDate.AddDays(2).Day || (SelectedDate.Day > 3 && day == SelectedDate.AddDays(-2).Day)) && (day != SelectedDate.AddDays(3).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-3).Day) || day != SelectedDate.AddDays(1).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-1).Day))))
               || (day != SelectedDate.Day && ((day == SelectedDate.AddDays(3).Day || (SelectedDate.Day > 3 && day == SelectedDate.AddDays(-3).Day)) && (day != SelectedDate.AddDays(2).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-2).Day) || day != SelectedDate.AddDays(1).Day || (SelectedDate.Day > 3 && day != SelectedDate.AddDays(-1).Day)))))
               && ((float)endprice > 0) && (SelectedDate.Month == date.Month))
        {}

Your ears and eyes can bleed now ;o)

Just to clarify... SelectedDate is the date passed to the calendar. And Day is Day in the month. (while loop day <= days) var date = new DateTime(SelectedDate.Year, SelectedDate.Month, 1);

Basically I'm passing a Date (say 27/11/2010 which is SelectedDay) which I need to test it. Is the SelectedDay the Current Calendar day being added to the string. If its not then if i add a day to the Selected Day does it match, then if not, two days, and again, three days.

But because its a date I have to check if its over day 3 before allowing it to check if it can match the day minus 3 (or the end of the last month will be used to mark the end of this month)

Answer

I used the following Syntax in the end.

        DateTime currentCalDate = DateTime.Parse(String.Format("{0}/{1}/{2}", day, SelectedDate.Month, SelectedDate.Year));
        int daysToAdd = (currentCalDate.Day + 3 < days) ? 3 : 0;
        int daysToDeduct = (currentCalDate.Day - 3 > 0) ? -3 : 0;

And

else if ((SelectedDate >= currentCalDate.AddDays(daysToDeduct) && SelectedDate <= currentCalDate.AddDays(daysToAdd)) && ((float)endprice > 0))

:o)

A: 

You can shorten first if stetement:

if (date.Date == now.Date)
grega g
+2  A: 

I think you can improve the second (long) part of the statement. By creating two separate date objects, one 3 days after the selected date and one 3 days in the future, you then can see if your day falls between these two points.

for example (in pseudo code):

if day > earlierDay && day < laterDay then
{
  //day falls within 3 days of selected day
}

Combine that with your other if conditions and that should be equivalent to what you have above (assuming I've understood your logic!)

UPDATE: With more information, I guess you could do something quite similar, but looking at the whole date object rather than just the day. I profess I am not an familiar with the .net syntax for date comparison, but if there is not one there already then I would write a short helper method that compares two dates to see if one is before the other. This can allow for months as well. This also means your if statement can be kept succinct. Wouldn't have to be anything clever but just a basic date comparison, checking year first, then month, then day.

chillysapien
Admittedly It started off as about 2 statements and in trying to fix an issue with multiple days being selected I ended up over complicating it to the point of being massively convoluted.
Chris M
Kind of right I need to compare the whole dates rather than just the days otherwise I have to deal with testing that were not adding 3 days to the end of the month or taking 3 days off the beginning of the month.
Chris M
I cheated See Answer Added to the Question :o)
Chris M
A: 

There is also some usless logic in the 2nd statement...

all 3 branches have

(day != SelectedDate.Day ...

Thats allways true, since you already managed to pass the beginning of the elseif which states

else if (((day == SelectedDate.Day)


Here is the reformated else block...

        {
            DateTime SelectedDate = DateTime.Now.AddDays(-3);

            float endprice = 14;

            int nextDay = SelectedDate.AddDays(1).Day;
            int prevDay = SelectedDate.AddDays(-1).Day;
            int dayAfterNext = SelectedDate.AddDays(2).Day;
            int dayBeforeYesterday = SelectedDate.AddDays(-2).Day;
            int dayThreeDaysAgo = SelectedDate.AddDays(-3).Day;
            if (((day == SelectedDate.Day)
                 ||
                 (
                     ((day == nextDay ||
                       (SelectedDate.Day > 3 && day == prevDay)) &&
                      (day != dayAfterNext ||
                       (SelectedDate.Day > 3 && day != dayBeforeYesterday) ||
                       day != SelectedDate.AddDays(3).Day
                       || (SelectedDate.Day > 3 && day != dayThreeDaysAgo))))
                 ||
                 (
                     ((day == dayAfterNext ||
                       (SelectedDate.Day > 3 && day == dayBeforeYesterday)) &&
                      (day != SelectedDate.AddDays(3).Day ||
                       (SelectedDate.Day > 3 && day != dayThreeDaysAgo) ||
                       day != nextDay ||
                       (SelectedDate.Day > 3 && day != prevDay)))) ||
                 (
                     ((day == SelectedDate.AddDays(3).Day ||
                       (SelectedDate.Day > 3 && day == dayThreeDaysAgo)) &&
                      (day != dayAfterNext ||
                       (SelectedDate.Day > 3 && day != dayBeforeYesterday) ||
                       day != nextDay ||
                       (SelectedDate.Day > 3 && day != prevDay)))))
                && (endprice > 0) && (SelectedDate.Month == date.Month))
            {
            }
        }
Heiko Hatzfeld
A: 
var selectedDate = DateTime.Now.Date.AddDays(-23);
var today = DateTime.Now.Date.AddDays(-22);

var check = new HashSet<int>();
foreach (var i in Enumerable.Range(-3,7))
  if (selectedDate.AddDays(i).ToString("yyyyMM") == today.ToString("yyyyMM"))
    check.Add(selectedDate.AddDays(i).Day);

if (selectedDate == today)
{
  // today
}
else if (check.Contains(selectedDate.Day))
{
  // within 3 days";
}

Is this regrettable?

takepara