DateTime.Now.DayOfWeek.ToString() 与 CultureInfo

发布于 2024-11-02 09:50:07 字数 134 浏览 5 评论 0原文

我有代码:

DateTime.Now.DayOfWeek.ToString()

这给了我英文的星期几名称,我想要德语版本,如何在此处添加 CultureInfo 来获取德语的星期几名称?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

森林散布 2024-11-09 09:50:07
var culture = new System.Globalization.CultureInfo("de-DE");
var day = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);
var culture = new System.Globalization.CultureInfo("de-DE");
var day = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek);
失去的东西太少 2024-11-09 09:50:07

您可以使用德语 CultureInfoDateTimeFormat.DayNames 属性。
例如:

CultureInfo german = new CultureInfo("de-DE");
string sunday = german.DateTimeFormat.DayNames[(int)DayOfWeek.Sunday];

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];
£烟消云散 2024-11-09 09:50:07

DayOfWeek 是一个枚举,因此它的 ToString 方法不区分区域性。

如果您坚持使用 DayOfWeek,您将需要编写一个函数将 Enum 值转换为相应的德语字符串:

string DayOfWeekGerman(DayOfWeek dow)
{

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

更好的方法是使用 ToString直接>日期时间:

CultureInfo german = new CultureInfo("de-DE");
string dayName = DateTime.Now.ToString("dddd", german);

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);
暮倦 2024-11-09 09:50:07

这是 Visual Basic 中的解决方案

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

Return GermanCultureInfo.DateTimeFormat.GetDayName(DayOfWeek.Sunday)

顺便说一下,该解决方案的功能已过时
DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("de-DE"))

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

翻了热茶 2024-11-09 09:50:07

我喜欢这个:

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

根据你的问题:

var culture = new System.Globalization.CultureInfo("de-DE");
var day = uiDateTime.GetDayOfWeek(culture);

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);
故事还在继续 2024-11-09 09:50:07
DateTime date = DateTime.Today;

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

Console.WriteLine(day); //Jueves

只需将“es-MX”更改为您想要的区域即可。

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.

指尖凝香 2024-11-09 09:50:07

您可以使用此代码以相同语言返回您的日期名称

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

在此处输入图像描述
注意:DateTime 返回一个 DayOfWeek 枚举,因此我使用代码从
另一个枚举

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文