Getting a Month Name Using Month Number in C#

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Get a Month Name
// from a Month Number
using 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));
    
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Get a Month Name
// from a Month Number
using 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));
    
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to Get a Month Name
// from a Month Number
using 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));
    
}

chevron_right


Output:

Abbreviated Month Name : Mar
Abbreviated Month Month : Jul




My Personal Notes arrow_drop_up

Small things always make you to think big

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : drone911

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.