Getting current culture day names in .NET

后端 未结 9 937
情书的邮戳
情书的邮戳 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条回答
  •  旧时难觅i
    2021-01-11 19:10

    I am posting this as a separate answer as it really has nothing to do with my other answer (which may be useful to someone else in the future in another context.)

    As an alternative to codeka's solution, you can also do something like this (which would avoid having to hard code the en-us day names.)

    string[] dayNamesNormal = culture.DateTimeFormat.DayNames;
    string[] dayNamesShifted = Shift(dayNamesNormal, (int)DayOfWeek.Monday);
    
    // you probably wanna add some error checking here.
    // this method shifts array left by a specified number
    // of positions, wrapping the shifted elements back to
    // end of the array
    private static T[] Shift(T[] array, int positions) {
        T[] copy = new T[array.Length];
        Array.Copy(array, 0, copy, array.Length-positions, positions);
        Array.Copy(array, positions, copy, 0, array.Length-positions);
        return copy;
    }
    

    I meant to post this sooner but I am fighting a dying external hard drive...

提交回复
热议问题