C # First month in uppercase

I show the name of the month:

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

However, when using Swedish names throughout the month in lowercase.

Is there any neat trick to make the first letter in upper case when formatting dates? Or do I need to write a function for it?

+7
source share
3 answers

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

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

I propose to clone the culture and redefine the new monthly 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 the string.Format method:

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

To make the months in uppercase, I use the TextInfo.ToTitleCase method.

+11
source

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

how does the capital date and month the first letter of ToLongDateString () lead to es-mx Culture?

0
source

All Articles