The purpose of the code below is to determine if a particular date qualifies as a "weekend" i.e after 12:00 PM on Thursday, minimum 2 days and before Monday 12:00 PM
Is there a better way? If-Else turns ugly and the Strategy pattern is way too much work for this.
public bool ValidateWeekend(DateTime pickupDate, DateTime dropoffDate)
{
TimeSpan ts = dropoffDate.Subtract(pickupDate);
if (ts.TotalDays >= 2 && ts.TotalDays <= 4)
{
switch (pickupDate.DayOfWeek)
{
case DayOfWeek.Thursday:
if (pickupDate.Hour >= 12)
{
switch (dropoffDate.DayOfWeek)
{
case DayOfWeek.Sunday:
return true;
case DayOfWeek.Monday:
if (dropoffDate.Hour <= 12)
{
return true;
}
return false;
}
}
break;
case DayOfWeek.Friday:
switch (dropoffDate.DayOfWeek)
{
case DayOfWeek.Sunday:
return true;
case DayOfWeek.Monday:
if (dropoffDate.Hour <= 12)
{
return true;
}
return false;
}
break;
case DayOfWeek.Saturday:
switch (dropoffDate.DayOfWeek)
{
case DayOfWeek.Sunday:
return true;
case DayOfWeek.Monday:
if (dropoffDate.Hour <= 12)
{
return true;
}
return false;
}
return false;
}
}
return false;
}