简体   繁体   English

如何在本地CultureInfo中获取DayOfWeek?

[英]How to get DayOfWeek in a local CultureInfo?

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
string day = dt.DayOfWeek.ToString();
day = day.Substring(0, 1).ToUpper();
MessageBox.Show(day); // result is "F"

How could I get the result in a local language (CultureInfo), for example - France and where can I find a list of languages references for this purpose ? 我如何以本地语言(CultureInfo)获得结果,例如-法国,在哪里可以找到为此目的使用的语言参考列表?

Assuming you've already got the CultureInfo , you can use: 假设您已经拥有CultureInfo ,则可以使用:

string dayName = dateTime.ToString("dddd", culture);

You'd then need to take the first character of it yourself - there's no custom date/time format for just that. 然后,您需要自己输入第一个字符-并没有自定义日期/时间格式。 You could use ddd for the abbreviated day name, but that's typically three characters, not one. 您可以使用ddd作为缩写的日期名称,但这通常是三个字符,而不是一个字符。

As for a list of cultures - you can use CultureInfo.GetCultures . 至于文化列表,您可以使用CultureInfo.GetCultures It will vary by platform, and could vary over time and even by version of .NET, too. 它会因平台而异,并且可能会随时间变化,甚至随.NET版本也会变化。

As an aside, this code isn't ideal: 顺便说一句,此代码并不理想:

DateTime dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

If you happen to call it just before midnight at the end of a year, you could get the "old" year but then January as the month. 如果你碰巧在今年年底午夜之前调用它,你可以得到“老字号”的一年,但随后1月担任了一个月。 You should evaluate DateTime.Now (or DateTime.Today ) once, and then use the same value twice: 您应该一次评估DateTime.Now (或DateTime.Today ),然后两次使用相同的值:

DateTime today = DateTime.Today;
DateTime startOfMonth = new DateTime(today.Year, today.Month, 1);

Straight from the MSDN: MSDN on DateTime with CultureInfo 直接来自MSDN: DateTime上的MSDN与CultureInfo

DateTime dt = DateTime.Now;
// Sets the CurrentCulture property to U.S. English.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// Displays dt, formatted using the ShortDatePattern
// and the CurrentThread.CurrentCulture.
Console.WriteLine(dt.ToString("d"));

Try dt.ToString("dddd") and then manipulate that string if necessary. 尝试dt.ToString("dddd") ,然后在必要时处理该字符串。 See Custom Date and Time Format Strings . 请参阅自定义日期和时间格式字符串

DateTime    dateValue    =    new    DateTime(2008,    6,    11);      
Console.WriteLine(dateValue.ToString("ddd",    
                  new    CultureInfo("fr-FR")));

it is as simple as this: 就这么简单:

var culture = new CultureInfo("da-DK");
var datestring =datetime.ToString("dd MMMMM yyyy kl. hh:mm",culture)

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

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