可能的重复: 如何在c#中获得MonthName ?

我使用下面的c#语法从月份中获取月份名称,没有,但是我得到了八月,我只想要八月。

System.Globalization.DateTimeFormatInfo mfi = new 
System.Globalization.DateTimeFormatInfo();
string strMonthName = mfi.GetMonthName(8).ToString();

任何的建议……


当前回答

你可以用下面的方法得到它,

DateTimeFormatInfo mfi = new DateTimeFormatInfo();
string strMonthName = mfi.GetMonthName(8).ToString(); //August

现在,获取前三个字符

string shortMonthName = strMonthName.Substring(0, 3); //Aug

其他回答

你需要GetAbbreviatedMonthName

将GetMonthName替换为GetAbbreviatedMonthName,使其为:

string strMonthName = mfi.GetAbbreviatedMonthName(8);

你也可以这样做来获取当前月份:

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);

这应该返回月份索引(1-12)中的月份文本(January - December)

int monthNumber = 1; //1-12  
string monthName = new DateTimeFormatInfo().GetMonthName(monthNumber);

对于短月份名称使用:

string monthName = new DateTime(2010, 8, 1)
    .ToString("MMM", CultureInfo.InvariantCulture);

对于西班牙文化(“es”)的长/完整月份名称:

string fullMonthName = new DateTime(2015, i, 1).ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));