Convert MonthName to MonthNo and vice versa

To convet monthname to month no. There are two approach are given below. 1)
  1. enum Months:byte
  2.     {
  3.         Jan = 1, Feb, March, April,
  4.         May, June, July, August, Sept, Oct, Nov, Dec
  5.     };
  6.  
  7.     public string MonthName(int month)
  8.     {
  9.         return Enum.GetName(typeof(Months), month);
  10.     }
  11.  
  12.     public byte monthNo(string monthName)
  13.     {
  14.         byte monthno=0;
  15.         foreach (byte Mno in Enum.GetValues(typeof(Months)))
  16.         {
  17.             if (Enum.GetName(typeof(Months), Mno) == monthName)
  18.             {
  19.                 monthno = Mno;
  20.                 break;
  21.                
  22.             }
  23.  
  24.         }
  25.         return monthno;
  26.     }
  27.  
  28. And call these method here
  29. Response.Write("Month Name:" + MonthName(12));
  30. Response.Write("Month No:" + monthNo("Dec").ToString());
2)import System. Globalization namespace then use this code
  1. DateTime.ParseExact("December", "MMMM", CultureInfo.CurrentCulture).Month.ToString();
  2. //return 12
  3. CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(12).ToString();
  4. //return December
Tags

Add new comment