具有CultureInfo的DateTime.Now.DayOfWeek.ToString()

我有这个代码:

DateTime.Now.DayOfWeek.ToString() 

那给我一个星期的英文名字,我想有德文版本,这里怎么加CultureInfo来得到德文的那个星期的名字?

 var culture = new System.Globalization.CultureInfo("de-DE"); var day = culture.DateTimeFormat.GetDayName(DateTime.Today.DayOfWeek); 

您可以使用德文CultureInfoDateTimeFormat.DayNames属性。 例如:

 CultureInfo german = new CultureInfo("de-DE"); string sunday = german.DateTimeFormat.DayNames[(int)DayOfWeek.Sunday]; 

这是Visual Basic中的解决scheme

 Dim GermanCultureInfo As Globalization.CultureInfo = New Globalization.CultureInfo("de-DE") Return GermanCultureInfo.DateTimeFormat.GetDayName(DayOfWeek.Sunday) 

解决scheme的function是DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("de-DE"))

DayOfWeek是一个枚举,所以它的ToString方法不是文化敏感的。

如果你坚持使用DayOfWeek ,你需要编写一个函数来把Enum的值转换成德文的对应string:

 string DayOfWeekGerman(DayOfWeek dow) { switch(dow) { case(DayOfWeek.Sunday) return "German Sunday"; case(DayOfWeek.Monday) return "German Monday"; ... } } 

更好的方法是直接使用DateTime ToString

 CultureInfo german = new CultureInfo("de-DE"); string dayName = DateTime.Now.ToString("dddd", german);