22

I am trying to print out the name of the month not the integer value of each month. (for example if the date is 2/2/2002, I would like the "month" to read out "February" instead of "2."

I am pulling in the system.DateTime.now to get the current month. When I try to print out the current month on the form, it just puts the correlating integer value for the month, whereas I would like to have the Month Name.

I know this can be done using switch-case or if statements to individually convert the numbers into word values, but I was just wondering if there was a simple, built-in conversion command that will automatically tell the application to print out the month's name instead of it's correlating integer value.

I am programming in the Visual Studio 2010 environment using C#-4.0, Silverlight-4.0.

AmbiguousX
  • 1,704
  • 4
  • 24
  • 39

3 Answers3

28

Try DateTime.Now.ToString("MMMM")

See MSDN: Custom Date and Time Format Strings for other format strings.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • Yes, I guess I just couldn't find the right combination of words to put in the search box because I couldn't find this page, this did the trick, though. Thank you and have a fantastic day! – AmbiguousX Aug 03 '10 at 20:52
  • 3
    You can also use that in XAML binding in Silverlight 4 by binding to a DateTime property and setting the "StringFormat" binding attribute to "MMMM" – Bytemaster Aug 03 '10 at 21:13
23

You can also get it like this:

var month = 7;
var dtf = CultureInfo.CurrentCulture.DateTimeFormat;
string monthName = dtf.GetMonthName(month);
string abbreviatedMonthName = dtf.GetAbbreviatedMonthName(month);

Resources:

Maksymilian Majer
  • 2,956
  • 2
  • 29
  • 42
4

If you want the full date (in your case: February 2, 2002) use: myDate.ToString("MMMM dd, yyyy")

For just the month: .ToString("MMMM") is correct.

AllenG
  • 8,112
  • 29
  • 40