Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CultureInfo For Swedish

I want to convert the datetime to Swedish Culture.

DateTime.Today.ToString("dd MMMM yyyy");

Above line of code gives me results as 27 December 2013

I want to have results which display december in swedish language.

like image 418
Manoj Sethi Avatar asked Dec 27 '13 07:12

Manoj Sethi


People also ask

What is CultureInfo?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.

What is CultureInfo CurrentCulture?

The CultureInfo. CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo. CurrentCulture property, as the following example illustrates. C# Copy.

When you specify a language but do not specify the associated country through a culture the culture is called as?

The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region. You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method.


2 Answers

And if you don't want to use the culture parameter everywhere you use this method, then you can set your applications default language to swedish by doing one or few of these:

CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("sv-SE");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("sv-SE");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("sv-SE");
Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");

Then anywhere you call your ToString() method it will stringify according to the current culture info that you set.

like image 98
Tolga Evcimen Avatar answered Sep 30 '22 10:09

Tolga Evcimen


You should use Swedish Culture for that:

DateTime.Today.ToString("dd MMMM yyyy", CultureInfo.GetCultureInfo("sv-SE"));

If Swedish should be used in each ToString() you can set up CurrentCulture:

  // Or/And CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("sv-SE");
  Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("sv-SE");
  ...

  // Since Current Culture is Swedish, there's no need to put it explicitly
  DateTime.Now.ToString("dd MMMM yyyy");    
like image 24
Dmitry Bychenko Avatar answered Sep 30 '22 09:09

Dmitry Bychenko