Koen
Koen

Reputation: 3686

Can Calendar.GetDayOfWeek() return (DayOfWeek)7?

I'm reviewing some code and found this bit (rewritten):

if ((int)CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(someDate) == 7) ...

I would think this condition always returns false since DayOfWeek (the return type) ranges from 0 to 6, or could this eventually return 7 in a specific culture?

Upvotes: 3

Views: 2634

Answers (3)

GvS
GvS

Reputation: 52518

Normally GetDayOfWeek will never return a (converted) value 7.

From the code it is very unclear what the programmer wants. I suggest rewriting it as:

if (CultureInfo.CurrentCulture.Calendar.GetDayOfWeek(someDate) == DayOfWeek.Saturday) ...

Or something.

Upvotes: 1

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

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

Source - http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx

Upvotes: 3

Itay Karo
Itay Karo

Reputation: 18286

Did you take a look at the DayOfWeek enum page on 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).

Upvotes: 2

Related Questions