310

Possible Duplicate:
How to get the MonthName in c#?

I used the following c# syntax to get month name from month no but i get August i want only Aug..

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

Any suggestion...

Community
  • 1
  • 1
bala3569
  • 10,832
  • 28
  • 102
  • 146
  • 5
    Aside from .GetMonthName(int) not returning the string you're looking for, it *does* return a string, so the .ToString() is unnecessary – DaveD Dec 18 '14 at 18:09
  • 1
    Wouldn't be easier just to add a .Substring(0,3)? – Federico Navarrete Jun 28 '17 at 11:57
  • Possible duplicate of [How to get the month name in C#?](https://stackoverflow.com/questions/975531/how-to-get-the-month-name-in-c) – Cœur Jul 10 '18 at 13:29

9 Answers9

421

For short month names use:

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

For long/full month names for Spanish ("es") culture:

string fullMonthName = new DateTime(2015, i, 1).ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));
John Smith
  • 7,243
  • 6
  • 49
  • 61
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • how would i use the code above if i want "August"? full month name? – Albert Laure Jan 28 '14 at 02:42
  • 27
    @User6675636b20796f7521 `string monthName = new DateTime(2010, 8, 1) .ToString("MMMM", CultureInfo.InvariantCulture);` – Robert Fricke Feb 19 '14 at 15:15
  • This: "string s = new DateTime(1958, month, 1).ToString("MMMM", CultureInfo.InvariantCulture());" gives me, "Non-invocable member 'System.Globalization.CultureInfo.InvariantCulture' cannot be used like a method." – B. Clay Shannon-B. Crow Raven Feb 02 '16 at 00:20
  • B. Clay Shannon add ( ) around (new DateTime(1958, month, 1)) – nuclear sweet Feb 05 '16 at 14:22
  • 2
    This answer is not the best IMHO to work from a month number instead of a DateTime like in OP question. Lasse V. Karlsen answer's is more straightforward for short month name. And to get the month name in your local culture, use CultureInfo.CurrentCulture like in CharithJ answer's. – AFract May 18 '16 at 08:35
  • Can I use it without `CultureInfo.InvariantCulture`? – Himalaya Garg May 09 '18 at 08:38
348

For Abbreviated Month Names : "Aug"

DateTimeFormatInfo.GetAbbreviatedMonthName Method (Int32)

Returns the culture-specific abbreviated name of the specified month based on the culture associated with the current DateTimeFormatInfo object.

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

For Full Month Names : "August"

DateTimeFormatInfo.GetMonthName Method (Int32)

Returns the culture-specific full name of the specified month based on the culture associated with the current DateTimeFormatInfo object.

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(8);
samgak
  • 23,944
  • 4
  • 60
  • 82
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • 2
    GetMonthName return full month name. If you need it as JAN, FEB, etc.. you could use var month = 10; string monthName = new DateTime(2014, month, 1).ToString("MMM", CultureInfo.InvariantCulture).ToUpper(); – Dmitry Pavlov Oct 23 '14 at 17:49
  • 4
    @DmitryPavlov `GetAbbreviatedMonthName()` is a better way to do that. – NickG Oct 02 '15 at 15:40
  • 2
    DateTimeFormatInfo should be DateTimeFormat (at least in .net 4.6) – TravisWhidden Nov 04 '16 at 19:11
  • 1
    Second option worked for me, although I used `GetCultureInfo("en-GB")` instead of `CurrentCulture` because I wanted to be sure, that my month name will always be in English – Maurice Klimek Jul 06 '17 at 11:31
88

Replace GetMonthName with GetAbbreviatedMonthName so that it reads:

string strMonthName = mfi.GetAbbreviatedMonthName(8);
Oliver
  • 43,366
  • 8
  • 94
  • 151
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
16
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)

This method return April

If you need some special language, you can add:

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

Or your preferred language.

For example, with es-ES culture:

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

Returns: Abril

Returns: Abril (in spanish, because, we configured the culture as es-ES in our webconfig file, else, you will get April)

That should work.

Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
13

You can get this in following way,

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

Now, get first three characters

string shortMonthName = strMonthName.Substring(0, 3); //Aug
CharithJ
  • 46,289
  • 20
  • 116
  • 131
Thili77
  • 1,061
  • 12
  • 20
  • 2
    Nice, although you don't need to ToString() the call to GetMonthName(), as it already returns a string. – Matt Aug 03 '21 at 02:15
12

You want GetAbbreviatedMonthName

AakashM
  • 62,551
  • 17
  • 151
  • 186
6

This should return month text (January - December) from the month index (1-12)

int monthNumber = 1; //1-12  
string monthName = new DateTimeFormatInfo().GetMonthName(monthNumber);
Faraj Farook
  • 14,385
  • 16
  • 71
  • 97
Rishin C
  • 71
  • 1
  • 1
6

You can also do this to get current Month :

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
bigtheo
  • 624
  • 9
  • 16
3
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);

Output

maj
may
Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24