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
2010-07-18 00:05:54
Yeah that worked
littleMan
2010-07-18 07:16:44
This is the best approach. But watch out for cultures that use a different calendar...
Richard
2010-07-18 10:10:49
+2
A:
Using a custom format string:
string name = new DateTime(2010,7,1).ToString("MMMM");
Guffa
2010-07-18 00:06:34
+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
2010-07-18 00:08:57
+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
2010-07-18 00:09:37
Huh... there wasn't a single answer when I started typing mine... well, at least we're all in agreement.
mattmc3
2010-07-18 00:14:12
A:
Dude!!
just have an string array containing names of 12 months, and names[7] is it.
irreputable
2010-07-18 00:15:51