C# · 12月 29, 2021

c# – 获取DayOfWeek值的本地化字符串

此代码未本地化: Enum.GetNames(typeof(DayOfWeek))

我想要一个返回本地化字符串列表的方法,从本地化的任意DayOfWeek开始,我想使用内置资源来实现.到目前为止,我已经提出了下面的代码,但是我觉得这样做应该不会像一个黑客一样受到支持.

public List<String> GetLocalizedDayOfWeekValues(DayOfWeek startDay = DayOfWeek.Sunday) { var days = new List<String>(); DateTime date = DateTime.Today; while (date.DayOfWeek != startDay) date.AddDays(1); for (int i = 0; i < 7; i++) days.Add(date.ToString(“dddd”)); return days; }

知道一个更好的方法,请分享.谢谢!

解决方法 我想你正在寻找 DateTimeFormatInfo.DayNames.示例代码: using System;using System.Globalization;class Program{ public static void Main() { var french = new CultureInfo(“FR-fr”); var info = french.DateTimeFormat; foreach (var dayName in info.DayNames) { // dimanche,lundi etc Console.WriteLine(dayName); } } }