我正在显示这样的月份名称:
String.Format("{0:MMMM}", DateTime.Now)
Run Code Online (Sandbox Code Playgroud)
但是,使用瑞典语时,所有月份的名称都是小写的.
在格式化日期时,是否有一些巧妙的技巧可以使第一个字母大写?或者我必须为它编写一个函数?
Ale*_*lex 13
我建议克隆一种文化并在其中重新定义一个新的月份名称:
var swedish = CultureInfo.GetCultureInfo("sv-SE");
swedish = (CultureInfo)swedish.Clone();
swedish.DateTimeFormat.MonthNames =
swedish.DateTimeFormat.MonthNames
.Select(m => swedish.TextInfo.ToTitleCase(m))
.ToArray();
swedish.DateTimeFormat.MonthGenitiveNames =
swedish.DateTimeFormat.MonthGenitiveNames
.Select(m => swedish.TextInfo.ToTitleCase(m))
.ToArray();
Run Code Online (Sandbox Code Playgroud)
然后在string.Format方法中使用它:
// date holds "Mars"
var date = String.Format(swedish, "{0:MMMM}", DateTime.Now);
Run Code Online (Sandbox Code Playgroud)
为了使大写数月,我使用TextInfo.ToTitleCase方法.
这里有一些很好的答案.如果你想要一个功能你可以写:
char.ToUpper(s[0]) + s.Substring(1);
Run Code Online (Sandbox Code Playgroud)