datetime.tostring 月份和日期语言

发布于 2024-12-28 22:03:27 字数 445 浏览 1 评论 0原文

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

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

为此,我正在做

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

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

如果人员文化是 IT,我需要显示“Martedi”和“Giugno”而不是“星期一”和“六月”,

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

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

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.

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

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

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

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

发布评论

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

评论(5

陪我终i 2025-01-04 22:03:27

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

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

这样,.ToString() 将尊重指定区域性的模式和名称

Those patterns describe year, month, day and other parameter locations in the output result of DateTime. But month and day names are taken from the CultureInfo object, not pattern. There's the DateTime.ToString() overload that supports passing the CultureInfo parameter along with the format.

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

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

可遇━不可求 2025-01-04 22:03:27

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

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

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

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

演示:

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

输出:

giovedì 26 gennaio 2012
den 26 januari 2012

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 D for long date pattern (as it will take the actual pattern from the format provider that you specify):

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
思念满溢 2025-01-04 22:03:27

我刚刚遇到了类似的问题并解决了它:

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

这要归功于 此链接。

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.

凉月流沐 2025-01-04 22:03:27

使用 DateTimeFormatInfo 类。

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

Use DateTimeFormatInfo class.

string code = "mk-MK";
DateTimeFormatInfo info =
    DateTimeFormatInfo.GetInstance(CultureInfo.GetCultureInfo(code));
string longDate = 
    DateTime.Now.ToString(info.LongDatePattern, new System.Globalization.CultureInfo(code));
眼趣 2025-01-04 22:03:27

DateTime.ToString() 还有另一个重载,您可以使用它指定所需的模式(如您当前的要使用的区域性。尝试:

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

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

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