Pergunta

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.

Foi útil?

Solução

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");    

Outras dicas

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.

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

refer here

// Creates and initializes the CultureInfo which uses the international sort.

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

// Creates and initializes the CultureInfo which uses the traditional sort.

DateTime.Today.ToString("dd MMMM yyyy",new CultureInfo(0x041D);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top