Getting current culture day names in .NET

后端 未结 9 967
情书的邮戳
情书的邮戳 2021-01-11 18:32

Is it possible to get the CurrentCulture\'s weekdays from DateTimeFormatInfo, but returning Monday as first day of the week instea

9条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-11 19:05

    You can Clone the current culture which gets a writable copy of the CultureInfo object. Then you can set the DateTimeFormat.FirstDayOfWeek to Monday.

    CultureInfo current = CultureInfo.Current;
    CultureInfo clone = (CultureInfo)current.Clone();
    
    clone.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Monday;
    

    The above clone will now treat Monday as the first day of the week.

    EDIT

    After re-reading your question I don't think this will do what you're expecting. The DayNames will still return in the same order regardless of the FirstDayOfWeek setting.

    But I'll leave this answer up as community wiki in case someone comes across this question in the future.

提交回复
热议问题