我只是尝试使用 DateTime结构将1到12之间的整数转换为缩写的月份名称。
DateTime
以下是我试过的方法:
DateTime getMonth = DateTime.ParseExact(Month.ToString(), "M", CultureInfo.CurrentCulture); return getMonth.ToString("MMM");
但是我在第一行得到一个 FormatException,因为字符串不是有效的 DateTime。有人能告诉我怎么做吗?
FormatException
You can do something like this instead.
return new DateTime(2010, Month, 1).ToString("MMM");
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);
See Here for more details.
Or
DateTime dt = DateTime.Now; Console.WriteLine( dt.ToString( "MMMM" ) );
Or if you want to get the culture-specific abbreviated name.
GetAbbreviatedMonthName(1);
Reference
var monthIndex = 1; return month = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(monthIndex);
You can try this one as well
CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName( Convert.ToInt32(e.Row.Cells[7].Text.Substring(3,2))).Substring(0,3) + "-" + Convert.ToDateTime(e.Row.Cells[7].Text).ToString("yyyy");