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
Recommended Posts:
- Getting the Total Number of Days in a Month Using If-else and Switch Statements in C#
- How to Get a Total Number of Days in a Month using built in Functions in C#?
- C# Program to Find the Number Occurring Odd Number of Times
- C# Program for Count Inversions in an array | Set 1 (Using Merge Sort)
- Program to swap numbers using XOR operator in C#
- C# Program for Minimum number of jumps to reach end
- C# Program to Count number of binary strings without consecutive 1's
- C# Program to Count trailing zeroes in factorial of a number
- C# Program for nth Catalan Number
- Program to find absolute value of a given number
- Program to Input Weekday Number and Print the Weekday in C#
- C# Program to Convert the Octal String to an Integer Number
- C# Program to Convert a Binary String to an Integer
- C# Program for Nested Conditional Operator
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

