Results 1 to 8 of 8

Thread: converting month string to number

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    15

    converting month string to number

    Hi all
    Im trying to find an easyer way to write this code

    Function Convert_Date_Str2Int(Date_Str As String) As Integer

    Select Case Date_Str
    Case "January"
    Convert_Date_Str2Int = 1
    Case "February"
    Convert_Date_Str2Int = 2
    Case "March"
    Convert_Date_Str2Int = 3
    Case "April"
    Convert_Date_Str2Int = 4
    Case "May"
    Convert_Date_Str2Int = 5
    Case "June"
    Convert_Date_Str2Int = 6
    Case "July"
    Convert_Date_Str2Int = 7
    Case "August"
    Convert_Date_Str2Int = 8
    Case "September"
    Convert_Date_Str2Int = 9
    Case "October"
    Convert_Date_Str2Int = 10
    Case "November"
    Convert_Date_Str2Int = 11
    Case "December"
    Convert_Date_Str2Int = 12
    End Select
    End Function

    its long and messey and im sure there is an easyer way to do it
    also is there a way to do the opposite comvert a month number to a string..

    Thanks ed

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

    Re: converting month string to number

    Quote Originally Posted by Edward233
    also is there a way to do the opposite comvert a month number to a string..
    VB Code:
    1. Private Sub Command1_Click()
    2. MsgBox MonthName(CLng(Text1.Text))
    3. End Sub
    Just put a month number in the textbox.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: converting month string to number

    A little use of the Format function should do the job:
    VB Code:
    1. Function Convert_Date_Str2Int(Date_Str As String) As Integer
    2.  
    3.   Convert_Date_Str2Int = CInt(Format("1 " & Date_Str & " 2006", "m"))
    4.  
    5. End Function

    VB Code:
    1. Function Convert_Date_Int2Str(Date_Int As Integer) As String
    2.  
    3.   Convert_Date_Int2Str = Format("2006 " & Date_Int & " 1", "mmmm")
    4.  
    5. End Function

  4. #4
    Member
    Join Date
    Feb 2006
    Posts
    48

    Re: converting month string to number

    To get the month-number of a given date use DateTime.Month(Date)
    Returns 1 for january, 2 for february, etc.

    In reverse, just format a numerical date with a partial long date format.

    i.e.

    VB Code:
    1. 'String to Number
    2.     Debug.Print DateTime.Month("15/march/2006")
    3.     Debug.Print DateTime.Month("15/march/06")
    4.     Debug.Print DateTime.Month("15/03/06")
    5.    
    6.     'Number to String
    7.     Debug.Print Format("01/03/2000", "mmmm")

    Output from that is -
    VB Code:
    1. 3
    2.  3
    3.  3
    4. March


    The day and year in the second part don't matter, as long as they are valid dates.

    Probably have to match the format with your Date format in "Start>Control Panel>Regional Settings>Date"

    Not that the other post above doesn't work - just an alternative.

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

    Re: converting month string to number

    Quote Originally Posted by Edward233
    Hi all
    Im trying to find an easyer way to write this code

    Function Convert_Date_Str2Int(Date_Str As String) As Integer

    Select Case Date_Str
    Case "January"
    Convert_Date_Str2Int = 1
    Case "February"
    Convert_Date_Str2Int = 2
    Case "March"
    Convert_Date_Str2Int = 3
    Case "April"
    Convert_Date_Str2Int = 4
    Case "May"
    Convert_Date_Str2Int = 5
    Case "June"
    Convert_Date_Str2Int = 6
    Case "July"
    Convert_Date_Str2Int = 7
    Case "August"
    Convert_Date_Str2Int = 8
    Case "September"
    Convert_Date_Str2Int = 9
    Case "October"
    Convert_Date_Str2Int = 10
    Case "November"
    Convert_Date_Str2Int = 11
    Case "December"
    Convert_Date_Str2Int = 12
    End Select
    End
    This is a little shorter code
    VB Code:
    1. Private Sub GetMonthNumber(pstrMonthName As String)
    2. Dim i As Long
    3. For i = 1 To 12
    4.   If pstrMonthName = MonthName(i) Then
    5.      MsgBox i
    6.   End If
    7. Next
    8. End Sub
    9.  
    10. Private Sub Command1_Click()
    11. GetMonthNumber "October"
    12. End Sub
    Last edited by Hack; Mar 15th, 2006 at 02:13 PM.

  6. #6
    Frenzied Member SeanK's Avatar
    Join Date
    May 2002
    Location
    Boston MA
    Posts
    1,160

    Re: converting month string to number

    Quote Originally Posted by Hack
    This is a little shorter code
    VB Code:
    1. Private Sub GetMonthNumber(pstrMonthName As String)
    2. Dim i As Long
    3. For i = 1 To 12
    4.   If pstrMonthName = MonthName(i) Then
    5.      MsgBox i
    6.   End If
    7. Next
    8. End Sub
    9.  
    10. Private Sub Command1_Click()
    11. GetMonthNumber "October"
    12. End Sub
    Very cool Hack. I've been using the Select Case to get the Month Name based on its number since forever.
    Beantown Boy
    Please use [highlight=vb]your code goes in here[/highlight] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: converting month string to number

    How about one-liner from me?
    VB Code:
    1. Function MonthStr2Int(sMonth As String) As Integer
    2.     MonthStr2Int = Month(sMonth & Space(1) & Year(Now))
    3. End Function
    4.  
    5. 'run the following loop to test:
    6. Private Sub Command1_Click()
    7. Dim i%
    8.  
    9.     For i = 1 To 12
    10.         Debug.Print MonthStr2Int(MonthName(i))
    11.     Next i
    12.  
    13. End Sub

  8. #8
    Addicted Member o0yuna0o's Avatar
    Join Date
    Mar 2006
    Posts
    172

    Re: converting month string to number

    use Month(Date_Str)

    if you want to get monthname
    Monthname([integer]) eg. Monthname(1) result: January
    If you want the 3 char month display do this
    Monthname(1,True) result: Jan



    Quote Originally Posted by Edward233
    Hi all
    Im trying to find an easyer way to write this code

    Function Convert_Date_Str2Int(Date_Str As String) As Integer

    Select Case Date_Str
    Case "January"
    Convert_Date_Str2Int = 1
    Case "February"
    Convert_Date_Str2Int = 2
    Case "March"
    Convert_Date_Str2Int = 3
    Case "April"
    Convert_Date_Str2Int = 4
    Case "May"
    Convert_Date_Str2Int = 5
    Case "June"
    Convert_Date_Str2Int = 6
    Case "July"
    Convert_Date_Str2Int = 7
    Case "August"
    Convert_Date_Str2Int = 8
    Case "September"
    Convert_Date_Str2Int = 9
    Case "October"
    Convert_Date_Str2Int = 10
    Case "November"
    Convert_Date_Str2Int = 11
    Case "December"
    Convert_Date_Str2Int = 12
    End Select
    End Function

    its long and messey and im sure there is an easyer way to do it
    also is there a way to do the opposite comvert a month number to a string..

    Thanks ed

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