Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

Name of day of week in italian language in c#

I can't to extract the name of day of week in italian language in my aspx page with c#.

I have tried this solution but in the output I have Friday, what wrong?

DateTime ItalianJobCookie = DateTime.Parse(Request.Cookies["ItalianJob"].Value);
string ItalianJobCookieDayName = ItalianJobCookie.DayOfWeek.ToString("G", new CultureInfo("it-IT"));

Response.Write(ItalianJobCookieDayName.ToString());

Upvotes: 4

Views: 3841

Answers (3)

Simone S.
Simone S.

Reputation: 1916

It's possible, replace this:

string ItalianJobCookieDayName = 
ItalianJobCookie.DayOfWeek.ToString("G", new CultureInfo("it-IT"));

with this:

string ItalianJobCookieDayName = 
new CultureInfo("it-IT").DateTimeFormat.GetDayName(ItalianJobCookie.DayOfWeek);

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

DateTime.DayOfWeek returns a DayOfWeek of a given DateTime which has no ToString that supports different cultures. This is even documented:

The members of the DayOfWeek enumeration are not localized. To return the localized name of the day of the week, call the DateTime.ToString(String) or the DateTime.ToString(String, IFormatProvider) method with either the "ddd" or "dddd" format strings. The former format string produces the abbreviated weekday name; the latter produces the full weekday name.

Instead use it with DateTime

string ItalianJobCookieDayName = ItalianJobCookie.ToString("dddd");

if the current culture is italian, otherwise:

string ItalianJobCookieDayName = ItalianJobCookie.ToString("dddd", new CultureInfo("it-IT"));

If you want the abbreviated day-name use ddd instead of dddd. Further informations.

Upvotes: 5

Soner Gönül
Soner Gönül

Reputation: 98750

DayOfWeek is culture invariant. It's always returns english-based day names.

From documentation;

The members of the DayOfWeek enumeration are not localized. To return the localized name of the day of the week, call the DateTime.ToString(String) or the DateTime.ToString(String, IFormatProvider) method with either the "ddd" or "dddd" format strings. The former format string produces the abbreviated weekday name; the latter produces the full weekday name.

You need to use dddd format specifier with it-IT culture.

string ItalianJobCookieDayName = ItalianJobCookie.ToString("dddd", new CultureInfo("it-IT"));

Upvotes: 4

Related Questions