【问题标题】:Get Month name from month number从月份编号获取月份名称
【发布时间】:2011-03-12 04:17:05
【问题描述】:

可能重复:
How to get the MonthName in c#?

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

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

任何建议...

【问题讨论】:

  • 除了 .GetMonthName(int) 不返回您要查找的字符串之外,它确实返回了一个字符串,因此 .ToString() 是不必要的
  • 仅仅添加一个 .Substring(0,3) 会不会更容易?

标签: c# .net datetime


【解决方案1】:

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

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

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

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

【讨论】:

  • 如果我想要“August”,我将如何使用上面的代码?完整的月份名称?
  • @User6675636b20796f7521 string monthName = new DateTime(2010, 8, 1) .ToString("MMMM", CultureInfo.InvariantCulture);
  • 这个:“string s = new DateTime(1958, month, 1).ToString("MMMM", CultureInfo.InvariantCulture());"给我,“不可调用的成员 'System.Globalization.CultureInfo.InvariantCulture' 不能像方法一样使用。”
  • B. Clay Shannon 在 (new DateTime(1958, month, 1)) 周围添加 ( )
  • 这个答案不是最好的恕我直言,从月份数字而不是像 OP 问题中的 DateTime 一样工作。 Lasse V. Karlsen 的答案对于短月份名称更为直接。要获取当地文化中的月份名称,请使用 CultureInfo.CurrentCulture,就像 CharithJ 的答案一样。
【解决方案2】:

对于缩写月份名称:“Aug”

DateTimeFormatInfo.GetAbbreviatedMonthName Method (Int32)

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

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

对于完整月份名称:“八月”

DateTimeFormatInfo.GetMonthName Method (Int32)

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

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

【讨论】:

  • GetMonthName 返回完整的月份名称。如果您需要它作为 JAN、FEB 等。您可以使用 var month = 10; string monthName = new DateTime(2014, month, 1).ToString("MMM", CultureInfo.InvariantCulture).ToUpper();
  • @DmitryPavlov GetAbbreviatedMonthName() 是一个更好的方法。
  • DateTimeFormatInfo 应该是 DateTimeFormat(至少在 .net 4.6 中)
  • 第二个选项对我有用,虽然我使用 GetCultureInfo("en-GB") 而不是 CurrentCulture 因为我想确定,我的月份名称将永远是英文
【解决方案3】:

GetMonthName 替换为GetAbbreviatedMonthName,使其变为:

string strMonthName = mfi.GetAbbreviatedMonthName(8);

【讨论】:

    【解决方案4】:
    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(西班牙语,因为我们在webconfig 文件中配置了culture as es-ES,否则,您将得到April

    应该可以的。

    【讨论】:

      【解决方案5】:

      您可以通过以下方式获得它,

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

      现在,获取前三个字符

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

      【讨论】:

      • 很好,尽管您不需要 ToString() 调用 GetMonthName(),因为它已经返回了一个字符串。
      【解决方案6】:

      你想要GetAbbreviatedMonthName

      【讨论】:

        【解决方案7】:

        这应该从月份索引(1-12)

        返回月份文本(January - December)
        int monthNumber = 1; //1-12  
        string monthName = new DateTimeFormatInfo().GetMonthName(monthNumber);
        

        【讨论】:

        • 请提供一些附加信息。不要只是发布代码
        【解决方案8】:
        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
        

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-02-19
            • 2015-09-25
            • 2017-10-03
            • 2015-11-29
            • 1970-01-01
            • 2017-08-15
            相关资源
            最近更新 更多