C#: Set initial DayOfWeek as Monday not Sunday

巧了我就是萌 提交于 2019-12-18 14:16:50

问题


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

(int)dateList[0].DayOfWeek == 0) // 0 = Sunday

回答1:


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




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/3135587/c-set-initial-dayofweek-as-monday-not-sunday

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