Reputation: 4387
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)
if (date.Year == now.Year && date.Month == now.Month && day == now.Day)
{
daysHtml.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)))))
&& ((double)endprice > 0) && (SelectedDate.Month == date.Month))
{}
Your ears and eyes can bleed now ;)
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 check:
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)
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)) && ((double)endprice > 0))
:)
Upvotes: 0
Views: 2352
Reputation: 10433
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?
Upvotes: 0
Reputation: 3197
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))
{
}
}
Upvotes: 0
Reputation: 2316
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.
Upvotes: 2