Returning the month for the given month name i.e. 1 for jan,2 for Feb and so on..

Vishalneeraj-24503
Posted by Vishalneeraj-24503 under Visual Studio category on | Points: 40 | Views : 1053
Write below code:-
public int GetMonth(string MonthName)
{
string[] Months = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
for (int Month = 0; Month < 12; Month++)
{
if (MonthName.Equals(Months[Month], StringComparison.OrdinalIgnoreCase))
return Month + 1;
}
return 0;
}


To test above function:-
Response.Write(GetMonth("jan") + "<br/>");
Response.Write("<br/>");
Response.Write(GetMonth("january") + "<br/>");
Response.Write("<br/>");
Response.Write(GetMonth("mar") + "<br/>");
Response.Write("<br/>");
Response.Write(GetMonth("October") + "<br/>");
Response.Write("<br/>");
Response.Write(GetMonth("apr") + "<br/>");
Response.Write("<br/>");
Response.Write(GetMonth("dec") + "<br/>");

Output:-
1
0
3
0
4
12

Comments or Responses

Login to post response