views:

101

answers:

5

e.g. example 7 is July

+13  A: 
// using the current culture - returns "July" for me
string x = DateTimeFormatInfo.CurrentInfo.GetMonthName(7);

// using a specific culture - returns "juillet"
string y = CultureInfo.GetCultureInfo("fr-FR").DateTimeFormat.GetMonthName(7);
LukeH
Yeah that worked
littleMan
This is the best approach. But watch out for cultures that use a different calendar...
Richard
+2  A: 

Using a custom format string:

string name = new DateTime(2010,7,1).ToString("MMMM");
Guffa
+1  A: 
  private static string GetMonthName(int month, bool abbrev)

  {

      DateTime date = new DateTime(1900, month, 1);

      if (abbrev) return date.ToString("MMM");

      return date.ToString("MMMM");

  }
Sadat
+1  A: 

Use the DateTime object's ToString() method. "MMMM" is the long month. "MMM" is a short month code like Aug for August. The nice thing is this way, you can deal with i18n issues too if you need to.

var monthID = 7;
var monthName = new DateTime(2000, monthID, 1).ToString("MMMM");
Console.WriteLine(monthName);
mattmc3
Huh... there wasn't a single answer when I started typing mine... well, at least we're all in agreement.
mattmc3
A: 

Dude!!

just have an string array containing names of 12 months, and names[7] is it.

irreputable
No, that would give you "August".
Guffa
@irreputable: And besides, why reinvent the wheel? `DateTimeFormatInfo.CurrentInfo.MonthNames[7]` does indeed give me "August".
LukeH