Set the language to Swedish

I have this in my partial view:

 <tr>
    <% for (int currentDay = 0; currentDay < 7; currentDay++)
       { %>
    <th>
    <%= System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[currentDay] %>
    </th>
    <% } %>
</tr>

The names of the days of the week are correctly displayed in Swedish, but somehow the week starts on Sunday, and the first day of the week in Sweden is Monday. How can i fix this?

And besides, is there any simple way to get him to transfer the first letter in the names of the days of the week in the form of capital letters?

+3
source share
3 answers

This is not strange, the DayOfWeek enumeration is defined as Sunday = 0. You must do it yourself using DateTimeFormatInfo.FirstDayOfWeekat System.Globalization.

The correct code is:

        CultureInfo ci = new CultureInfo("sv-SE");
        int substraction = (int)ci.DateTimeFormat.FirstDayOfWeek;

        int dayToGet = 0; //should return monday

        var daynames = ci.DateTimeFormat.DayNames;

        string day = daynames[dayToGet + substraction >= 7
            ? (dayToGet + substraction - 7) : dayToGet+substraction];

, , , str.Substring(0,1).ToUpper() + str.Substring(1), char .

+4

, DayNames. "" "". , .

: -

string dayname = myCulture.DateTimeFormat.DayNames[myCulture.DateTimeFormat.FirstDayOfWeek]

FirstDayOfWeek ? Ans: 1
dayname? : ""
, , 1 DayNames "", , , 0, "".

+1

You can do something like this:

for (int currentDay = 0; currentDay < 7; currentDay++)
{
    int currentLocalizedDay = ((int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek + currentDay) % 7;

    Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.DayNames[currentLocalizedDay]);
}

Or by changing the source code to something like this:

<tr>
    <% for (int currentDay = (int)CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; currentDay < 7; currentDay = (currentDay + 1 % 7))
       { %>
    <th>
    <%= System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[currentDay] %>
    </th>
    <% } %>
</tr>
0
source

Source: https://habr.com/ru/post/1722320/


All Articles