Convert MonthName to MonthNo and vice versa
Submitted by jitendramcu on Monday, December 7, 2009 - 15:36.
To convet monthname to month no. There are two approach are given below.
1)
2)import System. Globalization namespace then use this code
- 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());
- DateTime.ParseExact("December", "MMMM", CultureInfo.CurrentCulture).Month.ToString();
- //return 12
- CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(12).ToString();
- //return December
Add new comment
- 12 views