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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With