Getting a Month Name Using Month Number in C#

26 Oct, 2020
Given a number N (1<= N <=12), the task is to get a month name from a month number in C#.
Examples:
Input: N = 2
Output: February
  
Input: N = 5
Output: May


Method 1: Using DateTime.ToString() Method: This method can be used to get the both full and abbreviated name of the month.
Step 1: Get the Number N.
Step 2: Create an object of DateTime using the DateTime Struct.
DateTime date = new DateTime(year, month, day);;


Step 3: Converts the value of the current DateTime object to its equivalent string representation.
// to get the Abbreviated month name 
string month_name = date.ToString("MMM");

// to get the full month name 
string month_name = date.ToString("MMMM");


Step 4: This string contains the name of the month.
Below is the implementation of the above approach:
C#
// C# program to Get a Month Name// from a Month Numberusing System;   public class GFG{         // function to get the abbreviated month name    static string getAbbreviatedName(int month)     {        DateTime date = new DateTime(2020, month, 1);                 return date.ToString("MMM");    }         // function to get the full month name    static string getFullName(int month)     {        DateTime date = new DateTime(2020, month, 1);                 return date.ToString("MMMM");    }         static public void Main ()    {         int N = 3;         Console.WriteLine("Full Month Name : " + getFullName(N));                 N = 7;        Console.WriteLine("Abbreviated Month : " + getAbbreviatedName(N));    } }
Output:
Full Month Name : March
Abbreviated Month : Jul


Method 2: Using GetMonthName() Method: This method is used to get the full name of the month.
Step 1: Get the Number N.
Step 2: Create an object of CultureInfo and then get the month name using DateTimeFormat.GetMonthName() method.
// to get the full month name
string monthName = CultureInfo.CurrentCulture.
DateTimeFormat.GetMonthName(month);


Step 3: This string contains the name of the month.
Below is the implementation of the above approach:
C#
// C# program to Get a Month Name// from a Month Numberusing System;using System.Globalization;   public class GFG{         // function to get the full month name    static string getFullName(int month)     {        return CultureInfo.CurrentCulture.            DateTimeFormat.GetMonthName            (month);    }         static public void Main ()    {         int N = 3;         Console.WriteLine("Full Month Name : " + getFullName(N));                 N = 7;        Console.WriteLine("Full Month Month : " + getFullName(N));    } }
Output:
Full Month Name : March
Full Month Month : July


Method 3: Using GetAbbreviatedMonthName() Method: This method is used to get the Abbreviated name of the month.
Step 1: Get the Number N.
Step 2: Create an object of CultureInfo and then get the month name using DateTimeFormat.GetAbbreviatedMonthName() method.
// to get the Abbreviated month name
string monthName = CultureInfo.CurrentCulture.
DateTimeFormat.GetAbbreviatedMonthName(month);


Step 3: This string contains the name of the month.
Below is the implementation of the above approach:
C#
// C# program to Get a Month Name// from a Month Numberusing System;using System.Globalization;   public class GFG{         // function to get the Abbreviated month name    static string getAbbreviatedName(int month)     {        return CultureInfo.CurrentCulture.            DateTimeFormat.GetAbbreviatedMonthName            (month);    }         static public void Main ()    {         int N = 3;         Console.WriteLine("Abbreviated Month Name : " + getAbbreviatedName(N));                 N = 7;        Console.WriteLine("Abbreviated Month Month : " + getAbbreviatedName(N));    } }
Output:
Abbreviated Month Name : Mar
Abbreviated Month Month : Jul


drone911
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.