In this article, we will learn how to get month number from full month name and from Month Abbreviations in c#.
If you need to convert a month number from a full month name such as January to 01, December to 12, then you use this code.
Here is the complete code to get the Month Number from Full Month Name:
using System;
using System.Globalization;
namespace Tutorialsrack
{
class Program
{
/* How to Get Month Number From Month Name in C# */
static void Main(string[] args)
{
Console.Write("Enter The Full Month Name to Get Month Number: ");
//Enter the Full Name of the Month
string MonthName = Console.ReadLine();
//To get Month Number From Month Name
int MonthNumber = GetMonthNumber_From_MonthName(MonthName);
//Print The Result
Console.WriteLine("Result: {0:0#}", MonthNumber);
//Hit ENTER to exit the program
Console.ReadKey();
}
public static int GetMonthNumber_From_MonthName(string monthname)
{
int monthNumber = 0;
monthNumber= DateTime.ParseExact(monthname, "MMMM", CultureInfo.CurrentCulture).Month;
return monthNumber;
}
}
}
Enter The Full Month Name to Get Month Number:: January
Result: 01
Enter the Month Abbreviations to Get Month Number: December
Result: 12
If we need to convert a month number from a month abbreviations name such as Jan to 01, Dec to 12, then you use this code.
Here is the complete code to get the Month Number from a month abbreviations:
using System;
using System.Globalization;
namespace Tutorialsrack
{
class Program
{
/* How to Get Month Number From Month Abbreviations in C# */
static void Main(string[] args)
{
Console.Write("Enter the Month Abbreviations to Get Month Number: ");
//Enter the Month Abbreviations such as Jan, Dec, etc
string MonthName = Console.ReadLine();
//To get Month Number From Month Abbreviations
string MonthNumber = GetMonthNumberFromAbbreviation(MonthName);
//Print The Result
Console.WriteLine("Result: {0}", MonthNumber);
//Hit ENTER to exit the program
Console.ReadKey();
}
public static string GetMonthNumberFromAbbreviation(string mmm)
{
string[] monthAbbrev = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames;
// Creates a TextInfo based on the "en-US" culture.
TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
string monthname = myTI.ToTitleCase(mmm.ToLower());
int index = Array.IndexOf(monthAbbrev, monthname) + 1;
return index.ToString("0#");
}
}
}
Enter the Month Abbreviations to Get Month Number: Jan
Result: 01
Enter the Month Abbreviations to Get Month Number: Feb
Result: 02
If you need to convert a month number from a month abbreviations name or a Full Month name such as Jan to 01, Dec to 12, January to 01, February to 02 then you use this code.
Here is the complete code to get the Month Number from a month abbreviations or from a full month name:
using System;
namespace Tutorialsrack
{
class Program
{
/* How to Get Month Number From Month Abbreviations or from a Full Month Name in C# */
static void Main(string[] args)
{
Console.Write("Enter the Month Abbreviations to Get Month Number: ");
//Enter the Month Abbreviations such as Jan, Dec, etc.
//OR Enter the Full Month Name such as January, August, etc.
string MonthName = Console.ReadLine();
//Print The Result
Console.WriteLine("Result: {0:0#}", DateTime.Parse("1," + MonthName + " 2020").Month);
//Hit ENTER to exit the program
Console.ReadKey();
}
}
}
Enter the Month Abbreviations or Full Month Name to Get Month Number: Jan
Result: 01
Enter the Month Abbreviations or Full Month Name to Get Month Number: January
Result: 01
I hope this article will help you to understand how to get the Month number From Full Month Name or from Month Abbreviations in C#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments