Loading

Convert MonthName to MonthNo and vice versa

Submitted by: 


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

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.