Note that there are some explanatory texts on larger screens.

plurals
  1. POIf Statement Hell, How to check if a date passed matches a date within 3 days but only if the date doesnt match the other conditions
    text
    copied!<p>As the garbled question says I'm basically looking for a tidier way to do the following snippet. (its used in a calendar for availability matching)</p> <pre><code> if (date.Year == now.Year &amp;&amp; date.Month == now.Month &amp;&amp; day == now.Day) { daysHtml.Append("&lt;td class=\"today\"&gt;" + day.ToString() + "&lt;/td&gt;"); } else if (((day == SelectedDate.Day) || (day != SelectedDate.Day &amp;&amp; ((day == SelectedDate.AddDays(1).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day == SelectedDate.AddDays(-1).Day)) &amp;&amp; (day != SelectedDate.AddDays(2).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-2).Day) || day != SelectedDate.AddDays(3).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-3).Day)))) || (day != SelectedDate.Day &amp;&amp; ((day == SelectedDate.AddDays(2).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day == SelectedDate.AddDays(-2).Day)) &amp;&amp; (day != SelectedDate.AddDays(3).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-3).Day) || day != SelectedDate.AddDays(1).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-1).Day)))) || (day != SelectedDate.Day &amp;&amp; ((day == SelectedDate.AddDays(3).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day == SelectedDate.AddDays(-3).Day)) &amp;&amp; (day != SelectedDate.AddDays(2).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-2).Day) || day != SelectedDate.AddDays(1).Day || (SelectedDate.Day &gt; 3 &amp;&amp; day != SelectedDate.AddDays(-1).Day))))) &amp;&amp; ((double)endprice &gt; 0) &amp;&amp; (SelectedDate.Month == date.Month)) {} </code></pre> <p>Your ears and eyes can bleed now ;)</p> <p>Just to clarify... SelectedDate is the date passed to the calendar. And Day is Day in the month. (while loop day &lt;= days) var date = new DateTime(SelectedDate.Year, SelectedDate.Month, 1);</p> <p>Basically I'm passing a Date (say 27/11/2010 which is SelectedDay) which I need to check: </p> <ul> <li>Is the SelectedDay the Current Calendar day being added to the string. </li> <li>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. </li> </ul> <p>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)</p> <hr> <h2>Answer</h2> <p>I used the following Syntax in the end.</p> <pre><code> DateTime currentCalDate = DateTime.Parse(String.Format("{0}/{1}/{2}", day, SelectedDate.Month, SelectedDate.Year)); int daysToAdd = (currentCalDate.Day + 3 &lt; days) ? 3 : 0; int daysToDeduct = (currentCalDate.Day - 3 &gt; 0) ? -3 : 0; </code></pre> <p>And</p> <pre><code>else if ((SelectedDate &gt;= currentCalDate.AddDays(daysToDeduct) &amp;&amp; SelectedDate &lt;= currentCalDate.AddDays(daysToAdd)) &amp;&amp; ((double)endprice &gt; 0)) </code></pre> <p>:)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload