Results 1 to 7 of 7

Thread: [RESOLVED] Get The Number Of The Month From Its Name

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Resolved [RESOLVED] Get The Number Of The Month From Its Name

    I know that I can get the number of a month by feeding VB's built in MonthName function the month number
    vb Code:
    1. 'returns November
    2. Msgbox MonthName(11)
    However, I need to the number of the month based on its name, and I could not find anything that would give me that.

    So, I wrote this function (I have the names of all 12 months in a dropdown combo).
    vb Code:
    1. Private Function GetMonthNumber(pstrMonthName As String) As Integer
    2. Dim i As Integer
    3. ReDim Months(1 To 12)
    4. ReDim MonthNumber(1 To 12)
    5. Months = Array("January", "February", "March", "April", "May", "June", "July", _
    6. "August", "September", "October", "November", "December")
    7. MonthNumber = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")
    8. For i = 0 To 11
    9.     If cboMonths.Text = Months(i) Then
    10.        GetMonthNumber = MonthNumber(i)
    11.        Exit For
    12.     End If
    13. Next
    14. End Function
    15.  
    16. 'and to get the number
    17. Private Sub cboMonths_Click()
    18. Dim iMonNum As Integer
    19. iMonNum = GetMonthNumber(cboMonths.Text)
    20. MsgBox iMonNum
    21. End Sub
    Works just fine but I can't help feeling I'm overlooking something.

    Am I having a senior moment? There is an easier way of doing this, right?

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Get The Number Of The Month From Its Name

    Your solution can be made to be multi-regional aware, but isn't as written.

    The following isn't either, unless the month names are already regional aware. But is far easier I think
    Code:
    GetMonthNumber = Month(CDate("1/March/2000"))
    Code:
    GetMonthNumber = Month(CDate("March/1/2000"))
    Last edited by LaVolpe; Nov 19th, 2010 at 10:31 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Get The Number Of The Month From Its Name

    Regional awareness is not an issue. This app is going to be used by one company in Michigan that formats all dates as mm/dd/yyyy.

    I knew there had to be a better way.

    Thanks!

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Get The Number Of The Month From Its Name

    Depending on the requirements you might also use something like:
    Code:
    Function MonthNum(ByVal MonthName As String) As Integer
        MonthNum = InStr("JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC", _
                         UCase$(Left$(MonthName, 3)))
        If MonthNum > 0 Then MonthNum = (MonthNum - 1) \ 3 + 1
    End Function
    Last edited by dilettante; Nov 19th, 2010 at 12:33 PM. Reason: Decided April and May really are months too.

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: [RESOLVED] Get The Number Of The Month From Its Name

    ^^ Oops, excluded a couple of months, but the logic is very valid
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: [RESOLVED] Get The Number Of The Month From Its Name

    Funny!

    Next time I'll copy and paste instead of retyping.

  7. #7
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] Get The Number Of The Month From Its Name

    Or pehaps;

    Code:
    Function MonthName2MonthNumber(Month As String) As Long
        
        Dim i As Long
        For i = 1 To 12
            If UCase$(Month) = UCase$(MonthName(i)) Or UCase(Month) = UCase$(MonthName$(i, True)) Then
                MonthName2MonthNumber = i
                Exit For
            End If
        Next
    
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width