18

Is there a way to set the first DayOfWeek as Monday = 0 not Sunday?

(int)dateList[0].DayOfWeek == 0) // 0 = Sunday
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
msfanboy
  • 5,273
  • 13
  • 69
  • 120

3 Answers3

11

You would need to create a custom culture, and specify the initial DayOfWeek as Monday. An instance of your custom culture would need to be set to whatever context is normally used to access culture information (i.e. Thread.CurrentCulture). The following article should get you started on creating a custom culture:

How to: Create Custom Cultures

EDIT:

I just reread your question, and I noticed something. You can not change the DayOfWeek property...that is simply an enumeration value. You would need to compare the DayOfWeek to the FirstDayOfWeek property of the CultureInfo.DateTimeFormat property:

dateList[0].DayOfWeek == Thread.CurrentCulture.DateTimeFormat.FirstDayOfWeek

By default, FirstDayOfWeek is Sunday. If you created a custom culture, it could be any day of the week you so choose (i.e. Monday).

jrista
  • 32,447
  • 15
  • 90
  • 130
  • +1: This is the correct approach to this. DayOfWeek is not a number, and treating it as an integer is problematic at best. – Reed Copsey Jun 28 '10 at 20:03
  • @Reed: Agreed. I was rather confused by the OP at first...took a couple reads to figure out what the intent was. – jrista Jun 28 '10 at 20:10
  • Does the first day of the week depend on the culture? Actually "A" first day is very subjective. A first day could depend on the job you have... working in a hospital it could be sunday. If you work in the office or being a teacher its rather monday ;-) – msfanboy Jun 29 '10 at 17:49
  • I will comment out your code in my code maybe I need it later, when my user need a firstdayOfWeek differntly from monday :) – msfanboy Jun 29 '10 at 18:06
  • @msfanboy: There are culturally-common "first day of the week" days. In America, the first day of the week from a common culture perspective is Sunday, with the end of the week being Saturday. From a work-week perspective, the first day of the week is Monday in most cases. There are a lot of existing cultures defined for .NET, however if you need to customize the details of a culture...such as providing a Work-Week Culture where the first DOW is Monday, you have that ability. Technically speaking, you could create a culture that specifically defines your own personal lifestyle. ;) – jrista Jun 29 '10 at 18:08
  • The link doesn't seem to be working anymore, it can be found at: http://msdn.microsoft.com/en-us/library/ms172469(v=vs.100).aspx – GrandMasterFlush Oct 19 '12 at 14:26
  • @GrandMasterFlush: Thanks. Fixed the broken link. – jrista Oct 19 '12 at 15:13
3

DayOfWeek is an enum and so you can't change it. I have, in the past, simply adjusted the value I store to compensate and you may have to do something similar. I needed 0 = Unset.

Nate Zaugg
  • 4,202
  • 2
  • 36
  • 53
1

Just to complete this for people searching this issue, here is my simple workaround (VB.NET):

Private Function GetTrueDayOfWeek(DayOfWeekUS As Int16)
    Dim RetVal As Int16 = 0
    If DayOfWeekUS = 0 Then
        RetVal = 7
    Else
        RetVal = DayOfWeekUS
    End If
    Return RetVal
End Function

Usage:

Dim TrueDayInWeek as int16
TrueDayInWeek = GetTrueDayOfWeek(dateList(0).DayOfWeek)
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41