Over a million developers have joined DZone.

Function To Return Month Integer From Month Name

·
// Takes in the month name as a string and returns the number of the month


        public int MonthToInt(string monthName)
        {
            int monthNum = 0;

            switch (monthName)
            {
                case "January":
                case "Jan":
                case "Jan.":
                    monthNum = 1;
                    break;
                case "February":
                case "Feb":
                case "Feb.":
                    monthNum = 2;
                    break;
                case "March":
                case "Mar":
                case "Mar.":
                    monthNum = 3;
                    break;
                case "April":
                case "Apr":
                case "Apr.":
                    monthNum = 4;
                    break;
                case "May":
                    monthNum = 5;
                    break;
                case "June":
                case "Jun":
                case "Jun.":
                    monthNum = 6;
                    break;
                case "July":
                case "Jul":
                case "Jul.":
                    monthNum = 7;
                    break;
                case "August":
                case "Aug":
                case "Aug.":
                    monthNum = 8;
                    break;
                case "September":
                case "Sep":
                case "Sep.":
                    monthNum = 9;
                    break;
                case "October":
                case "Oct":
                case "Oct.":
                    monthNum = 10;
                    break;
                case "November":
                case "Nov":
                case "Nov.":
                    monthNum = 11;
                    break;
                case "December":
                case "Dec":
                case "Dec.":
                    monthNum = 12;
                    break;
                default:
                    break;
            }

            return monthNum;
        }
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}