从月份编号中获取月份名称

时间:2010-07-06 06:58:09

标签: c# .net datetime

  

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

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

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

任何建议......

9 个答案:

答案 0 :(得分:309)

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

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

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

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

答案 1 :(得分:253)

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

DateTimeFormatInfo.GetAbbreviatedMonthName Method (Int32)

  

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

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

完整月份名称:“八月”

DateTimeFormatInfo.GetMonthName Method (Int32)

  

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

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

答案 2 :(得分:83)

GetMonthName替换为GetAbbreviatedMonthName,使其显示为:

string strMonthName = mfi.GetAbbreviatedMonthName(8);

答案 3 :(得分:12)

您想要GetAbbreviatedMonthName

答案 4 :(得分:8)

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

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

现在,获取前三个字符

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

感谢。

答案 5 :(得分:7)

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-ES文件中配置了webconfig,否则,您将获得April

这应该有效。

答案 6 :(得分:2)

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

返回月份文本(1月-12月)
Private Sub Worksheet_Change(ByVal Target As Range)

    LastRow = Range("L4000").End(xlUp).Row
    ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
        Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
        :=xlSortNormal
    ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
        Range("B2:B" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
        :="BSUH (September),Frimley (October),CWH (November),Kingston (December)", _
        DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Endorsed Candidates").Sort.SortFields.Add Key:= _
        Range("C2:C4000"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder _
        :="Allen,Christine,Feri,Hubert,Paula", DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Endorsed Candidates").Sort
        .SetRange Range("A2:L" & LastRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

答案 7 :(得分: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

答案 8 :(得分:1)

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

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
相关问题