DateTime.Now.DayOfWeek.ToString() with CultureInfo
Asked Answered
S

7

79

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?

Shockey answered 19/4, 2011 at 13:2 Comment(0)
D
147
var culture = new System.Globalization.CultureInfo("de-DE");
var day = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);
Deniable answered 19/4, 2011 at 13:11 Comment(0)
S
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];
Shandishandie answered 25/8, 2011 at 1:50 Comment(0)
H
6

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);
Hertahertberg answered 19/4, 2011 at 13:7 Comment(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.Jeneejenei
R
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"))

Revenant answered 29/6, 2012 at 21:37 Comment(0)
M
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);
Mizzen answered 25/10, 2018 at 7:10 Comment(0)
K
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.

Kingsley answered 31/8, 2022 at 15:17 Comment(0)
O
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

Onagraceous answered 9/7, 2018 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.