Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# First monthletter in uppercase

Tags:

c#

date-format

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?

like image 814
Lasse Edsvik Avatar asked Mar 29 '11 11:03

Lasse Edsvik


2 Answers

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.

like image 122
Oleks Avatar answered Sep 23 '22 18:09

Oleks


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

char.ToUpper(s[0]) + s.Substring(1);
like image 22
Magnus Avatar answered Sep 21 '22 18:09

Magnus