Question: I have the following code, which simulates a week start on Saturday and then divides the week in 2 parts, returning Tuesday if the given date is Sat-Tue, else it returns Fri. I would like to know if this can be done with fewer math operations?

```
private static DateTime SemiWeeklyEndDate(DateTime date)
{
    if (((7 + (date.DayOfWeek - DayOfWeek.Saturday)) % 7) <= ((7 + (DayOfWeek.Tuesday - DayOfWeek.Saturday)) % 7))
        return date.AddDays((((int)DayOfWeek.Tuesday - (int)date.DayOfWeek + 7) % 7));

    return date.AddDays((((int)DayOfWeek.Friday - (int)date.DayOfWeek + 7) % 7));
}

```

Answer: 
You can create something a bit more readable by using local functions:

```
private static DateTime SemiWeeklyEndDate(DateTime date)
{
    int DaysBetween(DayOfWeek from, DayOfWeek to) => (to - from + 7) % 7;
    int DaysToEndOfWeek(DayOfWeek from) => DaysBetween(from, DayOfWeek.Saturday);

    var pastMidWeek = DaysToEndOfWeek(date.DayOfWeek) <= DaysToEndOfWeek(DayOfWeek.Tuesday);

    var daysOffset =
        pastMidWeek
            ? DaysBetween(DayOfWeek.Tuesday, date.DayOfWeek)
            : DaysBetween(DayOfWeek.Friday, date.DayOfWeek);

    return date.AddDays(daysOffset);
}

```

I think this version conveys the behavior you're after without excessive modulo maths.
This is how I like doing these type of problems

```
            DateTime jan1 = new DateTime(DateTime.Now.Year, 1, 1);
            DateTime firstSaturday = jan1.AddDays(6 - (int)jan1.DayOfWeek);

            DateTime today = DateTime.Now.Date;
            int daysFromSemiWeekBegin = today.Subtract(firstSaturday).Days % 14;

            DateTime semiWeekEnd = (daysFromSemiWeekBegin == 0) ? today : today.AddDays(14 - daysFromSemiWeekBegin);

```