77

I have the code:

DateTime.Now.DayOfWeek.ToString()

That give's me the english day of the week name, I want to have the german version, how to add CultureInfo here to get the german day of the week name?

PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

7 Answers7

144
var culture = new System.Globalization.CultureInfo("de-DE");
var day = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);
Diego
  • 19,494
  • 4
  • 32
  • 46
14

You can use the DateTimeFormat.DayNames property of the german CultureInfo. For example:

CultureInfo german = new CultureInfo("de-DE");
string sunday = german.DateTimeFormat.DayNames[(int)DayOfWeek.Sunday];
Jean-Baptiste
  • 141
  • 1
  • 2
6

This is the solution in Visual Basic

Dim GermanCultureInfo As Globalization.CultureInfo = New Globalization.CultureInfo("de-DE")

Return GermanCultureInfo.DateTimeFormat.GetDayName(DayOfWeek.Sunday)

The function of the solution is Obsolete by the way DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("de-DE"))

Falko
  • 17,076
  • 13
  • 60
  • 105
Despota
  • 175
  • 2
  • 8
5

DayOfWeek is an enumeration, so the ToString method on it is not culture sensitive.

You will need to write a function to convert the Enum value to a corresponding string in German, if you insist on using DayOfWeek:

string DayOfWeekGerman(DayOfWeek dow)
{

    switch(dow)
    {
      case(DayOfWeek.Sunday)
         return "German Sunday";
      case(DayOfWeek.Monday)
         return "German Monday";
      ...
    }
}

A better approach is to use ToString from DateTime directly:

CultureInfo german = new CultureInfo("de-DE");
string dayName = DateTime.Now.ToString("dddd", german);
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    The article mentions `DateTime.ToString(String)` or the `DateTime.ToString(String, IFormatProvider)` for the localized name - there's no need to write out a function for it. – Ian Pugsley Apr 19 '11 at 13:11
3

I like this one:

public static class DateTimeExtension
{
    public static string GetDayOfWeek(this DateTime uiDateTime, CultureInfo culture = null)
    {
        if (culture == null)
        {
            culture = Thread.CurrentThread.CurrentUICulture;
        }

        return culture.DateTimeFormat.GetDayName(uiDateTime.DayOfWeek);
    }
}

And according to your question:

var culture = new System.Globalization.CultureInfo("de-DE");
var day = uiDateTime.GetDayOfWeek(culture);
isxaker
  • 8,446
  • 12
  • 60
  • 87
2
DateTime date = DateTime.Today;

string day = date.ToString("dddd", new CultureInfo("es-MX"));

Console.WriteLine(day); //Jueves

only change "es-MX" for the region you want.

0

You can use this code to return your day name as same language

CultureInfo myCI = new CultureInfo("ar-EG");   
MessageBox.Show(myCI.DateTimeFormat.GetDayName(DayOfWeek.Friday));

enter image description here note: DateTime returns a DayOfWeek Enumeration so I use the code to return from another Enumeration

Zoe
  • 27,060
  • 21
  • 118
  • 148