Search code examples
c#datecalendardayofweek

DayOFweek add int value in c#


Is there any way that with a DayOFweek object, with for example, value monday, that i can add +1 int value, so now the object value equals tuesday? Like for example:

DayOfWeek dayofweek = DayOfWeek.Monday;
dayofweek = dayofweek.addDay(1);

I've invented this code so you can understand what i mean. Hope you can help me. Thank you in advance.


Solution

  • You can just add a number directly:

    DayOfWeek dayofweek = DayOfWeek.Monday;
    dayofweek = (DayOfWeek)((int)(dayofweek + 1) % 7); //this will be tuesday
    

    Edit: adjusted to account for Saturday->Sunday