|
-
Mar 15th, 2006, 08:32 AM
#1
Thread Starter
New Member
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
-
Mar 15th, 2006, 08:38 AM
#2
Re: converting month string to number
 Originally Posted by Edward233
also is there a way to do the opposite comvert a month number to a string..
VB Code:
Private Sub Command1_Click()
MsgBox MonthName(CLng(Text1.Text))
End Sub
Just put a month number in the textbox.
-
Mar 15th, 2006, 08:38 AM
#3
Re: converting month string to number
A little use of the Format function should do the job:
VB Code:
Function Convert_Date_Str2Int(Date_Str As String) As Integer
Convert_Date_Str2Int = CInt(Format("1 " & Date_Str & " 2006", "m"))
End Function
VB Code:
Function Convert_Date_Int2Str(Date_Int As Integer) As String
Convert_Date_Int2Str = Format("2006 " & Date_Int & " 1", "mmmm")
End Function
-
Mar 15th, 2006, 11:10 AM
#4
Member
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:
'String to Number
Debug.Print DateTime.Month("15/march/2006")
Debug.Print DateTime.Month("15/march/06")
Debug.Print DateTime.Month("15/03/06")
'Number to String
Debug.Print Format("01/03/2000", "mmmm")
Output from that is -
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.
-
Mar 15th, 2006, 02:02 PM
#5
Re: converting month string to number
 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:
Private Sub GetMonthNumber(pstrMonthName As String)
Dim i As Long
For i = 1 To 12
If pstrMonthName = MonthName(i) Then
MsgBox i
End If
Next
End Sub
Private Sub Command1_Click()
GetMonthNumber "October"
End Sub
Last edited by Hack; Mar 15th, 2006 at 02:13 PM.
-
Mar 16th, 2006, 02:16 PM
#6
Frenzied Member
Re: converting month string to number
 Originally Posted by Hack
This is a little shorter code 
VB Code:
Private Sub GetMonthNumber(pstrMonthName As String)
Dim i As Long
For i = 1 To 12
If pstrMonthName = MonthName(i) Then
MsgBox i
End If
Next
End Sub
Private Sub Command1_Click()
GetMonthNumber "October"
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.
-
Mar 16th, 2006, 10:48 PM
#7
Re: converting month string to number
How about one-liner from me?
VB Code:
Function MonthStr2Int(sMonth As String) As Integer
MonthStr2Int = Month(sMonth & Space(1) & Year(Now))
End Function
'run the following loop to test:
Private Sub Command1_Click()
Dim i%
For i = 1 To 12
Debug.Print MonthStr2Int(MonthName(i))
Next i
End Sub
-
Mar 17th, 2006, 03:11 AM
#8
Addicted Member
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
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|