DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Function To Return Month Integer From Month Name

08.20.2008
| 3979 views |
  • submit to reddit
        // 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;
        }
    

Comments

Snippets Manager replied on Thu, 2009/04/23 - 3:41pm

how about this instead: public int MonthToInt(string monthName) { int monthNum = 0; string month = monthName.ToUpper(); if (month.StartsWith("JA")) { monthNum = 1; } if (month.StartsWith("F")) { monthNum = 2; } if (month.StartsWith("MAR")) { monthNum = 3; } if (month.StartsWith("AP")) { monthNum = 4; } if (month.StartsWith("MAY")) { monthNum = 5; } if (month.StartsWith("JUN")) { monthNum = 6; } if (month.StartsWith("JUL")) { monthNum = 7; } if (month.StartsWith("AU")) { monthNum = 8; } if (month.StartsWith("S")) { monthNum = 9; } if (month.StartsWith("O")) { monthNum = 10; } if (month.StartsWith("N")) { monthNum = 11; } if (month.StartsWith("D")) { monthNum = 12; } return monthNum; }