senzacionale
senzacionale

Reputation: 20916

mvc3 and culture info for date and day names

In globa.asax.cs i use this code for cultre

protected void Application_BeginRequest(object sender, EventArgs e)
{
    CultureInfo newCulture = new CultureInfo("sl-SI");
    Thread.CurrentThread.CurrentCulture = newCulture;
    Thread.CurrentThread.CurrentUICulture = newCulture;
}

but when i write

@DateTime.Today.DayOfWeek i always get

"Sunday"

instead of our language

"Nedelja"

. I am using mvc3. how can i get our date and day names?

Upvotes: 0

Views: 741

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500365

DayOfWeek is an enum, and the enum's ToString value will always simply give you the name of the value. You need something like:

string day = newCulture.DateTimeFormat.DayNames[(int) DateTime.Today.DayOfWeek];

EDIT: To make this simpler from your view, you might want a new extension method:

public static class DayOfWeekExtensions
{
    public static string ToCurrentCultureString(this DayOfWeek dayOfWeek)
    {
        // Or CurrentUICulture... I can never remember which way round they are.
        var formatInfo = CultureInfo.CurrentCulture.DateTimeFormat;

        return formatInfo.DayNames[(int) dayOfWeek];
    }
}

Then in your view:

@DateTime.Today.DayOfWeek.ToCurrentCultureString()

Upvotes: 4

Related Questions