开发者

c# First monthletter in uppercase

开发者 https://www.devze.com 2023-02-20 07:40 出处:网络
I\'m displaying a month name like this: String.Format(\"{0:MMMM}\", DateTime.Now) However, when using Swedish all month names are in lowercase.

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?


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.


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

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


There are various solutions; the answers to this question have some good ones.

how to uppercase date and month first letter of ToLongDateString() result in es-mx Culture?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号