converting a month name to month ordinal

Hi:
   How can I convert a month's name into a ordinal number? The months are three lettters and need to be converted to number value:
jan = 01
feb =02
mar =03
etc

thanks
LuckyLucksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gopinathdeepakCommented:
its only 12 cases....i would just use a select case..

private sub ReturnMtnhOrdinal(byval MonthName as string) as integer
  dim MonthOrdinal as integer
  select case MonthName.ToLowerString
    case "jan"
      monthOrdinal = 1
    case "feb"
      monthOrdinal = 2
    case "mar"
      monthOrdinal = 3
    case "apr"
      monthOrdinal = 4
    case "may"
      monthOrdinal = 5
    case "jun"
      monthOrdinal = 6
    case "jul"
      monthOrdinal = 7
    case "aug"
      monthOrdinal = 8
    case "sep"
      monthOrdinal = 9
    case "oct"
      monthOrdinal = 10
    case "nov"
      monthOrdinal = 11
    case "dec"
      monthOrdinal = 12
    case else
      monthordinal = 0
  End Select

  Return MonthOrdinal

end sub
dbfromnewjerseyCommented:
Where are the three-letter month names coming from?  Are they being read from a file?  Entered into textboxes on a form?

You could define the three-letter names as class variables and set them equal to the numbers

i.e.  Dim jan as integer = 01
       Dim feb as integer = 02
      etc.



angelfeijooCommented:
If you have previously validated the month:

nMonth = Int("janfebmaraprmayjunjulaugsepoctnovdec".IndexOf(sMonthName.ToLower) / 3) + 1

If not:

nMonth = Int("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|".IndexOf(sMonthName.ToLower & "|") / 4) + 1

In both cases an invalid month will return 0

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Or...

    Private Function GetMonthOrdinal(ByVal month As String) As Integer
        Return Array.IndexOf("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec".Split("|".ToCharArray), month.ToLower) + 1
    End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
   Dim month As Date = "01 Feb"
    Dim number As Integer = month.ToString("MM")


Bob
planoczCommented:
Sample with Enums.....

   Enum Months
        Jan = 1
        Feb = 2
        Mar = 3
        Apr = 4
        May = 5
        Jun = 6
        Jul = 7
        Aug = 8
        Sep = 9
        Oct = 10
        Nov = 11
        Dec = 12
    End Enum

        Dim s As String
        For Each s In [Enum].GetNames(NumMonths)
            Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(NumMonths, [Enum].Parse(NumMonths, s), "d"))
        Next s
        'OUTPUT DATA
        'Jan = 1
        'Feb = 2
        'Mar = 3
        'Apr = 4
        'May = 5
        'Jun = 6
        'Jul = 7
        'Aug = 8
        'Sep = 9
        'Oct = 10
        'Nov = 11
        'Dec = 12
Bob LearnedCommented:
I guess this all goes to prove that there are so many ways to skin a cat :)

Bob
planoczCommented:
Sorry forgot this

        Dim NumMonths As Type = GetType(Months)
planoczCommented:
Hey, go for it Bob
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
If you really needed that leading zero then...

    Private Function GetMonthOrdinal(ByVal month As String) As String
        Return (Array.IndexOf("jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec".Split("|".ToCharArray), month.ToLower) + 1).ToString("00")
    End Function
SanclerCommented:
Can anyone play?

    Private Function MonthToOrdinal(ByVal m As String) As Integer
        Dim s As String = "01/" & m & "/2005"
        Try
            Dim d As Date = s
            Return d.Month
        Catch ex As Exception
            MsgBox("Invalid Date")
            Return -1
        End Try
    End Function

Catches location differences and full month names as well.

Roger
Bob LearnedCommented:
Yes, Roger, I think that anyone can play ;)

I think we done shot some serious holes in this one :)

Bob
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
I like yours the best Roger!

I like seeing all the creative answers though and it's nice to see so many responses when most of these experts know they won't get the points for such a simple question...

Personally, I wanted to see how much code I could cram into a one liner function.   ;)
Bob LearnedCommented:
ROFL :))

I like Roger's best, too :)

Mike, you win the longest single line prize :)

Bob
planoczCommented:
I agree I like Roger's answer. :)
Bob LearnedCommented:
In fact, Roger's code is going into the code vault :)

Bob
Bob LearnedCommented:
Simplification complete:

    Public Shared Function MonthNameToOrdinal(ByVal name As String) As Integer

      If IsDate("01 " & name) Then
        Return DateTime.Parse("01 " & name).Month
      Else
        Return -1
      End If

    End Function 'MonthNameToOrdinal'

Notice the missing Exception handling, the missing year, and the missing date separator.

Bob
planoczCommented:
Leave it to Bob to come up with a short cut! lolol :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.