简体   繁体   English

c# 文化信息的日期格式问题

[英]c# date formatting issue with Culture info

I try to format a local date into a string, but based on the local format.我尝试将本地日期格式化为字符串,但基于本地格式。 By example, for the 5 May 2020, if I use French, I expected "5 Mai, 2020", but I got "5 May, 2020"...例如,对于 2020 年 5 月 5 日,如果我使用法语,我期望“5 Mai, 2020”,但我得到“5 May, 2020”...

The month name is still in English !月份名称还是英文的!

Here is my code to test this:这是我的代码来测试这个:

https://dotnetfiddle.net/saZJ0S https://dotnetfiddle.net/saZJ0S

Do you have any idea?你有什么主意吗?

Thx谢谢

You need to use the DateTime.ToString overload that gets CultureInfo parameter as a second argument.您需要使用获取 CultureInfo 参数作为第二个参数的DateTime.ToString重载。

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=netcore-3.1

So your code would look like (copying from the image you attached):所以你的代码看起来像(从你附加的图像复制):

string FormattedPostDate = localDate.ToString(CultureInfo.GetCultureInfo("fr-FR").DateTimeFormat.MonthDayPattern, new System.Globalization.CultureInfo("fr-FR")) + ", " + localDate.Year;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM