304

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...

3
  • 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, 2014 at 18:09
  • 1
    Wouldn't be easier just to add a .Substring(0,3)? Jun 28, 2017 at 11:57
  • Possible duplicate of How to get the month name in C#?
    – Cœur
    Jul 10, 2018 at 13:29

9 Answers 9

415

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"));
6
  • how would i use the code above if i want "August"? full month name? Jan 28, 2014 at 2:42
  • 26
    @User6675636b20796f7521 string monthName = new DateTime(2010, 8, 1) .ToString("MMMM", CultureInfo.InvariantCulture); Feb 19, 2014 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." Feb 2, 2016 at 0:20
  • B. Clay Shannon add ( ) around (new DateTime(1958, month, 1)) Feb 5, 2016 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, 2016 at 8:35
341

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);
4
  • 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(); Oct 23, 2014 at 17:49
  • 3
    @DmitryPavlov GetAbbreviatedMonthName() is a better way to do that.
    – NickG
    Oct 2, 2015 at 15:40
  • 2
    DateTimeFormatInfo should be DateTimeFormat (at least in .net 4.6) Nov 4, 2016 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 Jul 6, 2017 at 11:31
86

Replace GetMonthName with GetAbbreviatedMonthName so that it reads:

string strMonthName = mfi.GetAbbreviatedMonthName(8);
0
15
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.

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
1
  • 2
    Nice, although you don't need to ToString() the call to GetMonthName(), as it already returns a string.
    – Matt
    Aug 3, 2021 at 2:15
12

You want GetAbbreviatedMonthName

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);
1
  • 1
    Please provide some additional information. Don't just post code
    – ochs.tobi
    Aug 14, 2018 at 7:24
6

You can also do this to get current Month :

string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.