Katherine
Katherine

Reputation: 319

DayOfWeek get the next DayOfWeek(Monday,Tuesday...Sunday)

Is there a way to summarize this code into 1-2 lines?

My goal is to return, for example, I have a DayOfWeek which is Monday, I want to get the day after that (Tuesday) or n days after that.

         switch (_RESETDAY)
        {
            case DayOfWeek.Monday:
                _STARTDAY = DayOfWeek.Tuesday;
                break;
            case DayOfWeek.Tuesday:
                _STARTDAY = DayOfWeek.Wednesday;
                break;
            case DayOfWeek.Wednesday:
                _STARTDAY = DayOfWeek.Thursday;
                break;
            case DayOfWeek.Thursday:
                _STARTDAY = DayOfWeek.Friday;
                break;
            case DayOfWeek.Friday:
                _STARTDAY = DayOfWeek.Saturday;
                break;
            case DayOfWeek.Saturday:
                _STARTDAY = DayOfWeek.Sunday;
                break;
            case DayOfWeek.Sunday:
                _STARTDAY = DayOfWeek.Monday;
                break;
            default:
                _STARTDAY = DayOfWeek.Tuesday;
                break;
        }

Upvotes: 6

Views: 2440

Answers (5)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

The general case solution uses modulo arithemtics:

  DayOfWeek _RESETDAY = ...;
  int shift = 1; // can be positive or negative

  // + 7) % 7 to support negative shift´s
  DayOfWeek result = (DayOfWeek) ((((int)_RESETDAY + shift) % 7 + 7) % 7);

probably, the best implmentation is to hide the combersome formula in the extension method:

 public static class DayOfWeekExtensions {
   public static DayOfWeekShift(this DayOfWeek value, int shift) {
     return (DayOfWeek) ((((int)value + shift) % 7 + 7) % 7);
   }
 }

 ...

 var result = _RESETDAY.Shift(1);

and slightly reduced (only works in all cases if negative shift isn't lower than -7) :

     return (DayOfWeek)(((int)value + shift + 7) % 7);

Upvotes: 0

Mulflar
Mulflar

Reputation: 424

    static DayOfWeek dayplus (DayOfWeek day)
    {
        if (day == DayOfWeek.Saturday)
            return DayOfWeek.Sunday;
        else
            return day + 1;
    }

For example

Console.WriteLine(dayplus(DayOfWeek.Sunday));

Will return monday

Upvotes: 0

Jonesopolis
Jonesopolis

Reputation: 25370

same result as the addition and modulo stuff answered above, but more readable imho:

day = (day == DayOfWeek.Saturday) ? DayOfWeek.Sunday : day + 1;

Obvious code intent is always more enjoyable.

Upvotes: 2

Andrei
Andrei

Reputation: 56688

This is just an int enumeration, ranging from Sunday (0) to Saturday (6), as per MSDN:

The DayOfWeek enumeration represents the day of the week in calendars that have seven days per week. The value of the constants in this enumeration ranges from DayOfWeek.Sunday to DayOfWeek.Saturday. If cast to an integer, its value ranges from zero (which indicates DayOfWeek.Sunday) to six (which indicates DayOfWeek.Saturday).

So simple math should do it:

DayOfWeek nextDay = (DayOfWeek)(((int)_RESETDAY + 1) % 7);

Replace + 1 with + n if that's what you need.

Upvotes: 10

Ian Newson
Ian Newson

Reputation: 7939

Yes.

(DayOfWeek)((int)(_RESETDAY+1)%7)

Upvotes: 4

Related Questions