C# datetime.tostring 月日语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9022100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:38:15  来源:igfitidea点击:

datetime.tostring month and day language

c#datetimetostringcultureinfo

提问by paolofabio

i have a list of email addresses of people that have different nationalities (for each person i have the iso code)

我有一个不同国籍的人的电子邮件地址列表(对于每个人,我都有 iso 代码)

when i send the email to all these people, in the text of the mail i need to to convert a datetime field to a string formatted in their specific culture.

当我向所有这些人发送电子邮件时,在邮件文本中,我需要将日期时间字段转换为以他们的特定文化格式化的字符串。

for this i'm doing

为此我正在做

CultureInfo ci = new CultureInfo(ISO);
myStringDate = myDate.ToString(ci.DateTimeFormat.ShortDatePattern);

and work perfect, but if i use LongDatePattern instead short, for displaying date like "Monday, 13 June 2010" its work fine except the language of the day and month.

并且工作完美,但如果我使用 LongDatePattern 而不是短,则显示日期如“2010 年 6 月 13 日,星期一”,除了日期和月份的语言外,它的工作正常。

if the person culture is it-IT i need to display "Martedi" and "Giugno" not "monday" and "June"

如果是个人文化,我需要显示“Martedi”和“Giugno”而不是“monday”和“June”

how can i do that without change the current UI culture?

我怎样才能在不改变当前 UI 文化的情况下做到这一点?

回答by Tomislav Markovski

Use DateTimeFormatInfoclass.

使用DateTimeFormatInfo类。

string code = "mk-MK";
DateTimeFormatInfo info =
    DateTimeFormatInfo.GetInstance(CultureInfo.GetCultureInfo(code));
string longDate = 
    DateTime.Now.ToString(info.LongDatePattern, new System.Globalization.CultureInfo(code));

回答by Guffa

You have only specified the format pattern, so it takes the other settings from the default format. You should also specify the format provider:

您只指定了格式模式,因此它采用默认格式的其他设置。您还应该指定格式提供程序:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern, ci);

or using the standard format string Dfor long date pattern (as it will take the actual pattern from the format provider that you specify):

或使用D长日期模式的标准格式字符串(因为它将从您指定的格式提供程序中获取实际模式):

myStringDate = myDate.ToString("D", ci);


Demo:

演示:

CultureInfo ci = new CultureInfo("it-IT");
Console.WriteLine(DateTime.Now.ToString("D", ci));
ci = new CultureInfo("sv-SE");
Console.WriteLine(DateTime.Now.ToString("D", ci));

Output:

输出:

giovedì 26 gennaio 2012
den 26 januari 2012

回答by AakashM

DateTime.ToString()has yet another overload with which you can specify both the pattern you want (as you are currently andthe culture to use. Try:

DateTime.ToString()还有另一个重载,您可以使用它指定您想要的模式(正如您当前要使用的文化。尝试:

myStringDate = myDate.ToString(ci.DateTimeFormat.LongDatePattern,
                               ci);

回答by archil

Those patterns describe year, month, day and other parameter locationsin output result of Datetime. But month and day namesare taken from CultureInfo object, not pattern. There's DateTime.ToString overloadthat supports passing CultureInfo parameter along with format

这些模式描述了 Datetime 输出结果中的年、月、日和其他参数位置。但是月份和日期名称取自 CultureInfo 对象,而不是模式。有DateTime.ToString 重载支持传递 CultureInfo 参数和格式

        CultureInfo culture = new CultureInfo(ISO); 
        DateTime.Now.ToString(culture.DateTimeFormat.LongDatePattern, culture);

This way, ToString() will respect both pattern and names from specified culture

这样, ToString() 将尊重来自指定文化的模式和名称

回答by Robert

I just had a similar problem and solved it with:

我刚刚遇到了类似的问题并通过以下方式解决了它:

DateTime.Now.ToString("dddd, dd MMMM yyyy", new System.Globalization.CultureInfo("it-IT"));

That was thanks to this link.

这要归功于这个链接。