从月份号获取月份名称


254

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

我使用以下c#语法从no月份获取月份名称,但我得到了 August我只想要Aug..

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

任何建议...


3
除了.GetMonthName(INT)没有返回你要找的字符串,它不会返回一个字符串,这样的ToString()是不必要的
DaveD

1
仅添加.Substring(0,3)会更容易吗?
Federico Navarrete

Answers:


349

对于短月份名称,请使用:

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

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

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

如果要“八月”,我将如何使用上面的代码?完整的月份名称?
艾伯特·劳尔2014年

25
@ User6675636b20796f7521string monthName = new DateTime(2010, 8, 1) .ToString("MMMM", CultureInfo.InvariantCulture);
罗伯特·弗里克

这:“字符串s =新的DateTime(1958,month,1).ToString(“ MMMM”,CultureInfo.InvariantCulture());“ 给我,“不可调用的成员'System.Globalization.CultureInfo.InvariantCulture'不能像方法一样使用。”
B. Clay Shannon

B.克莱·香农((DateTime(1958,month,1))前后加上()
甜蜜的核

2
这个答案不是最好的恕我直言,而不是像OP问题中那样,使用月数而不是DateTime。对于简短的月份名称,Lasse V. Karlsen的答案更为简单。要获得您当地文化中的月份名称,请像在CharithJ答案中一样使用CultureInfo.CurrentCulture。
AFract

290

缩写月份名称:“ Aug”

DateTimeFormatInfo.GetAbbreviatedMonthName方法(Int32)

根据与当前DateTimeFormatInfo对象关联的区域性,返回指定月份的区域性特定于文化的缩写名称。

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(8)

全月名称:“八月”

DateTimeFormatInfo.GetMonthName方法(Int32)

根据与当前DateTimeFormatInfo对象关联的区域性,返回指定月份的区域性特定于全名的名称。

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(8);

2
GetMonthName返回完整的月份名称。如果需要使用JAN,FEB等,则可以使用var month = 10; 字符串monthName = new DateTime(2014,month,1).ToString(“ MMM”,CultureInfo.InvariantCulture).ToUpper();
德米特里·帕夫洛夫

3
@DmitryPavlov GetAbbreviatedMonthName()是一种更好的方法。
NickG

2
DateTimeFormatInfo应该为DateTimeFormat(至少在.net 4.6中)
TravisWhidden

第二种选择对我有用,尽管我曾经 GetCultureInfo("en-GB")的,而不是CurrentCulture因为我想可以肯定,我的月份名称将永远是英语
莫里斯Klimek

这应该是最好的答案。拯救了我的一天。谢谢。
Mahmoud Eidarous



11

您可以通过以下方式获得此信息,

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

现在,获取前三个字符

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

9
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)

此方法返回 April

如果需要某种特殊语言,可以添加:

<system.web>
    <globalization culture="es-ES" uiCulture="es-ES"></globalization>
     <compilation debug="true"
</system.web>

或您的首选语言。

例如,es-ES文化:

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)

返回值: Abril

返回:(Abril以西班牙语表示,因为我们culture as es-ESwebconfig文件中配置,否则,您将获得April

那应该工作。


5

这应该从月份索引(1-12)返回月份文本(一月-十二月

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

1
请提供一些其他信息。不要只是发布代码
ochs.tobi,

1
var month = 5;
var cultureSwe = "sv-SE";
var monthSwe = CultureInfo.CreateSpecificCulture(cultureSwe).DateTimeFormat.GetAbbreviatedMonthName(month);
Console.WriteLine(monthSwe);

var cultureEn = "en-US";
var monthEn = CultureInfo.CreateSpecificCulture(cultureEn).DateTimeFormat.GetAbbreviatedMonthName(month);
Console.WriteLine(monthEn);

输出量

maj
may
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.