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

流过昼夜 提交于 2019-12-10 13:25:11

问题


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;
        }

回答1:


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.




回答2:


Yes.

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



回答3:


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.




回答4:


    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




回答5:


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);


来源:https://stackoverflow.com/questions/32826723/dayofweek-get-the-next-dayofweekmonday-tuesday-sunday

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!