Share via


Convert short month to integer month

Question

Monday, June 8, 2009 9:23 AM

Any function to convert string Jan..... Dec to interger value 1....12? Thank you.

All replies (8)

Monday, June 8, 2009 6:34 PM ✅Answered | 2 votes

This works for me:

            int monthIndex;
            string desiredMonth = "May";
            string[] MonthNames=CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;
            monthIndex = Array.IndexOf(MonthNames, desiredMonth) + 1;

Hope this helps.www.insteptech.com
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!


Tuesday, June 9, 2009 6:00 AM ✅Answered | 1 vote

You got the example from Deborah, use AbbreviatedMonthNames instead.  Don't try .NET programming without the MSDN library at your fingertips.
Hans Passant.


Tuesday, June 9, 2009 3:34 AM

Thank you, it work only if full month name is given, for example December but not Dec.


Tuesday, June 9, 2009 3:41 AM

If you know the full date then,

 

DateTime dt = DateTime.Parse("02-Jan-2009");

 

int monthNo = dt.Month;

Prakash Subramani (MCAD)


Tuesday, June 9, 2009 4:15 AM

Use AbbreviatedMonthNames instead.
Hans Passant.


Tuesday, June 9, 2009 5:55 AM

Any example?


Tuesday, June 9, 2009 6:09 AM

Thank you.

To all, the anwser is :

string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.AbbreviatedMonthNames;


Thursday, September 5, 2013 1:36 AM

Just one simple line of code...

int month = DateTime.ParseExact("Dec", "MMM", CultureInfo.CurrentCulture).Month;