Alex Rivera | Logout

c# First monthletter in uppercase

Asked 2011-03-29T11:58:34.283
10

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?

Edit
Report

1 Answer

17

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.

answered 2011-03-29T12:21:56.163

Your Answer