;

How to Get a Month Name from a Month Number in C#


Tutorialsrack 03/04/2020 C# Programs

In this article, we will learn how to get a month's name from the month number in c#. In this article, we will use DateTime formatting and DateTimeFormat class methods that belong to System.Globalization.CultureInfo.CurrentCulture get a month's name from the month number. Here is some example to get a month name from the month number as follows:

Example 1: In this example, we will use the date formatting to get the full month name from a month number.

Example 1
using System;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get Month Name From Month Number in C# */
		static void Main(string[] args)
        {
			Console.WriteLine("Month Name using Month Number: " + MonthName(2));

			Console.WriteLine("Month Name using Month Number: " + MonthName(5));

			Console.WriteLine("Month Name using Month Number: " + MonthName(12));
			//Hit ENTER to exit the program
			Console.ReadKey();
		}
		private static string MonthName(int month)
		{
			DateTime date = new DateTime(2020, month, 1);
			return date.ToString("MMMM");
		}
	}
}

 

Output

Month Name using Month Number: February

Month Name using Month Number: May

Month Name using Month Number: December

Example 2: In this example, we will use the date formatting to get the Abbreviated month name from a month number.

Example 2
using System;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get a Abbreviated Month Name From Month Number in C# */
		static void Main(string[] args)
        {
			Console.WriteLine("Month Name using Month Number: " + Abbreviated_MonthName(2));

			Console.WriteLine("Month Name using Month Number: " + Abbreviated_MonthName(5));

			Console.WriteLine("Month Name using Month Number: " + Abbreviated_MonthName(12));
			//Hit ENTER to exit the program
			Console.ReadKey();
		}
		private static string Abbreviated_MonthName(int month)
		{
			DateTime date = new DateTime(2020, month, 1);
			return date.ToString("MMM");
		}
	}
}
Output

Month Name using Month Number: Feb

Month Name using Month Number: May

Month Name using Month Number: Dec

Example 3: In this example, we will use the CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName() and this function takes 1 argument which is month Number and for using this function, we need to include System.Globalization namespace.

Example 3
using System;
using System.Globalization;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get Month Name From Month Number in C# */
		static void Main(string[] args)
        {
			Console.WriteLine("Month Name using Month Number: " + monthName(2));

			Console.WriteLine("Month Name using Month Number: " + monthName(5));

			Console.WriteLine("Month Name using Month Number: " + monthName(12));
			//Hit ENTER to exit the program
			Console.ReadKey();
		} 
		
		public static string monthName(int monthNumber)
		{
			string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNumber);
			return monthName;
		}	
    }
}
Output

Month Name using Month Number: February

Month Name using Month Number: May

Month Name using Month Number: December

Example 4: In this example, we will use the CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName() and this function takes 1 argument which is month Number and for using this function, we need to include System.Globalization namespace.

Example 4
using System;
using System.Globalization;

namespace Tutorialsrack
{
    class Program
    {
		/* How to Get Abbreviated Month Name From Month Number Using System.Globalization in C# */
		static void Main(string[] args)
        {
			Console.WriteLine("Month Name using Month Number: " + Abbreviated_monthName(2));

			Console.WriteLine("Month Name using Month Number: " + Abbreviated_monthName(5));

			Console.WriteLine("Month Name using Month Number: " + Abbreviated_monthName(12));
			//Hit ENTER to exit the program
			Console.ReadKey();
		} 
		
		public static string Abbreviated_monthName(int monthNumber)
		{
			string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(monthNumber);
			return monthName;
		}	
    }
}
Output

Month Name using Month Number: Feb

Month Name using Month Number: May

Month Name using Month Number: Dec

I hope this article will help you to understand how to get a month's name from a month number in C#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags