Skip to content

Converting short date format text to numbered month

alps

Asked Aug 25, 2005, 1:42 AM EDT3 answers
Share
LinkedInX
Hi all.

How do I convert string format month int numbered month.

For example how do i convert "Apr" into 05, or "Jan into 01.

Thanks in advance.

Alps

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    public static int convertmonth (string month)
    {
    switch (month)
    {
    case "Jan":
    return 1;
    break;
    case .....
    ...
    case "Dec":
    return 12;
    break;
    default:
    return null;
    break;
    }
    }
    0
  • 0
    Thanks, The method you coded, is the obvious way.

    However I was thinking if a method already exists in the .NET classes, 
    may be in the DateTime classes somewhere.
    0
  • 0
    You could also use

    string stringmonth = "Feb"; //or whatever you wish

    int month = Convert.ToDateTime("01/"+stringmonth+"/2005").Month;

    I think the first method was better though.

    I´m not sure if there are other ways.

    0