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;
}
Use DateTime. DayOfWeek property to display the current day of week. DayOfWeek wk = DateTime. Today.
In English, the names are Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday, then returning to Monday. Such a week may be called a planetary week.
String[] days = new String[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; Display the day name.
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.
Yes.
(DayOfWeek)((int)(_RESETDAY+1)%7)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With