On Several occasions, you might need to get month name from month number or from current month number to month name in C#, so in this article, I have provided C# code with sample console application which shows, how you can get month name in C#.
Get month name using C# in Console application
We will create extension class with methods and pass month number to get full month name or short month name.
public static class MonthExtensions
{
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);
}
}
In the above code, we have created two methods, inside "MonthExtensions
" class, both of these methods have parameters which take Datetime value and from the passed datetime we extract full or short month name using DateTimeFormat
methods.
Complete C# Code
using System;
using System.Globalization;
public class Program
{
public static void Main()
{
//get current month name
Console.WriteLine(DateTime.Now.ToMonthName());
Console.WriteLine(DateTime.Now.ToShortMonthName());
//get previous month name
var OlderDate= DateTime.ParseExact("22/10/2019","dd/MM/yyyy",CultureInfo.InvariantCulture);
Console.WriteLine(OlderDate.ToMonthName());
Console.WriteLine(OlderDate.ToShortMonthName());
}
}
//create datetime extension class
public static class MonthExtensions
{
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);
}
}
Output:
January
Jan
October
Oct
As you can see in the above example, we are using "Date
" method to get abbreviated name of the specified month and "Date
Getting Month name using DateTime Format Specifier
There is one more easy method to get month name from current datetime or any datetime property in C#, which is using .ToString() with proper format.
For example,
string month = dateTime.ToString("MMMM");
//output, considering current month in Datetime is jan
//January
Let's take a look at an example, which shows how to take month short name, full name using C#
using System;
public class MonthProgram
{
public static void Main()
{
//get current month number
Console.WriteLine(DateTime.Now.ToString("MM"));
//get current month name, short hand
Console.WriteLine(DateTime.Now.ToString("MMM"));
//get current month name, full name
Console.WriteLine(DateTime.Now.ToString("MMMM"));
}
}
Output:
01
Jan
January
Get Month Name using System.Globalization.CultureInfo.CurrentCulture
You can also simply pass month number and then month name using , like below
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)
This will return output as "April".
Or if you need it in any other language then in English, you can simply add this in web.Config
<system.web>
<globalization culture="es-ES" uiCulture="es-ES"></globalization>
</system.web>
Now, above C# code, will return month name in spanish langauge as "Abril", instead of "April"
You may also like to read: