Convert MonthName to MonthNo and vice versa
To convet monthname to month no. There are two approach are given below.
1)
enum Months:byte { Jan = 1, Feb, March, April, May, June, July, August, Sept, Oct, Nov, Dec }; public string MonthName(int month) { return Enum.GetName(typeof(Months), month); } public byte monthNo(string monthName) { byte monthno=0; foreach (byte Mno in Enum.GetValues(typeof(Months))) { if (Enum.GetName(typeof(Months), Mno) == monthName) { monthno = Mno; break; } } return monthno; } And call these method here Response.Write("Month Name:" + MonthName(12)); Response.Write("Month No:" + monthNo("Dec").ToString());
2)import System. Globalization namespace then use this code
DateTime.ParseExact("December", "MMMM", CultureInfo.CurrentCulture).Month.ToString(); //return 12 CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(12).ToString(); //return December

Add new comment