c# First Monthletter in uppercase

发布于 2024-10-28 08:36:29 字数 165 浏览 4 评论 0原文

我显示的月份名称如下:

String.Format("{0:MMMM}", DateTime.Now) 

但是,使用瑞典语时,所有月份名称均为小写。

格式化日期时是否有一些巧妙的技巧可以将首字母变为大写?或者我必须为其编写一个函数?

I'm displaying a month name like this:

String.Format("{0:MMMM}", DateTime.Now) 

However, when using Swedish all month names are in lowercase.

Is there some neat trick to make first letter uppercase when formatting dates? Or do I have to write a function for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-11-04 08:36:29

我建议克隆一种文化并在其中重新定义一个新的月份名称:

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();

然后在 string.Format 方法中使用它:

// date holds "Mars"
var date = String.Format(swedish, "{0:MMMM}", DateTime.Now);

为了使月份大写,我使用 TextInfo.ToTitleCase 方法。

I'd suggest to clone a culture and re-define a new month names in it:

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();

and then use it in string.Format method:

// date holds "Mars"
var date = String.Format(swedish, "{0:MMMM}", DateTime.Now);

To make months in upper case I use TextInfo.ToTitleCase method.

站稳脚跟 2024-11-04 08:36:29

这里已经有一些很好的答案。如果你想要一个函数,你可以写:

char.ToUpper(s[0]) + s.Substring(1);

There are some good answers here already. If you want a function you can write:

char.ToUpper(s[0]) + s.Substring(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文