问题
How do you get a month name from an integer value?
e.g. 7 is July
回答1:
// 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);
回答2:
Using a custom format string:
string name = new DateTime(2010,7,1).ToString("MMMM");
回答3:
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);
回答4:
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");
}
回答5:
Dude!!
just have an string array containing names of 12 months, and names[7] is it.
来源:https://stackoverflow.com/questions/3273747/how-do-you-get-a-month-name-from-an-integer-value