How does one go about finding the month name in C#? I don't want to write a huge switch
statement or if
statement on the month int
. In VB.Net you can use MonthName()
, but what about C#?
Asked
Active
Viewed 1.9e+01k times
112

johnnyRose
- 7,310
- 17
- 40
- 61
-
Possible duplicate of [Best way to turn an integer into a month name in c#?](http://stackoverflow.com/questions/218908/best-way-to-turn-an-integer-into-a-month-name-in-c) – bluish Apr 29 '16 at 09:17
-
Does this answer your question? [Get Month name from month number](https://stackoverflow.com/questions/3184121/get-month-name-from-month-number) – StayOnTarget Jul 08 '21 at 14:02
6 Answers
186
You can use the CultureInfo to get the month name. You can even get the short month name as well as other fun things.
I would suggestion you put these into extension methods, which will allow you to write less code later. However you can implement however you like.
Here is an example of how to do it using extension methods:
using System;
using System.Globalization;
class Program
{
static void Main()
{
Console.WriteLine(DateTime.Now.ToMonthName());
Console.WriteLine(DateTime.Now.ToShortMonthName());
Console.Read();
}
}
static class DateTimeExtensions
{
public static string ToMonthName(this DateTime dateTime)
{
return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);
}
public static string ToShortMonthName(this DateTime dateTime)
{
return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);
}
}
Hope this helps!

CodeLikeBeaker
- 20,682
- 14
- 79
- 108
-
5Might I add that there is an `InvariantInfo` property that can be used as well. And, in my opinion, the following is a more simple/readable format for doing this: `DateTimeFormatInfo.InvariantInfo.GetAbbreviatedMonthName(...)` or `DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(...)` – bsara Feb 04 '13 at 23:57
132
Use the "MMMM" format specifier:
string month = dateTime.ToString("MMMM");

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
19assuming you have a date. if not: `var month = new DateTime(1,i,1).ToString("MMMM");` – Myster Jul 11 '11 at 04:21
15
string CurrentMonth = String.Format("{0:MMMM}", DateTime.Now)

George Stocker
- 57,289
- 29
- 176
- 237
-
-
6Or String.Format, actually. Just DateTime.Now.ToString("MMMM") is simpler. – Jon Skeet Jun 10 '09 at 13:32
-
1I would have suggested that as well, but you already had that as another answer. Gortok's method also demonstrates the use of placeholders to do the formatting, so I think it's a good alternative example. – Richard Anthony Hein Jun 10 '09 at 19:20
7
If you just want to use MonthName then reference Microsoft.VisualBasic and it's in Microsoft.VisualBasic.DateAndTime
//eg. Get January
String monthName = Microsoft.VisualBasic.DateAndTime.MonthName(1);

RobV
- 28,022
- 11
- 77
- 119
-19
private string MonthName(int m)
{
string res;
switch (m)
{
case 1:
res="Ene";
break;
case 2:
res = "Feb";
break;
case 3:
res = "Mar";
break;
case 4:
res = "Abr";
break;
case 5:
res = "May";
break;
case 6:
res = "Jun";
break;
case 7:
res = "Jul";
break;
case 8:
res = "Ago";
break;
case 9:
res = "Sep";
break;
case 10:
res = "Oct";
break;
case 11:
res = "Nov";
break;
case 12:
res = "Dic";
break;
default:
res = "Nulo";
break;
}
return res;
}
-
12This does not answer the question of the OP, because they said: "I really do **not** want to write a huge switch statement [...]" (emphasis mine). Please read questions in detail before writing an answer. – honk Sep 27 '15 at 09:36