Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Month Name to Corresponding Integer Value 1

Status
Not open for further replies.

sbushway

Programmer
Jan 27, 2003
58
0
0
US
I know I'm probably missing something incredibly simple, but how can I convert a month name into its integer equivalent (ie: September would be 9)

I'm creating a new date. I've got the year and the day in the correct format. The format for the month needs to be the integer value - and I've only got the month name.

Code:
New Date(Me.ddlDPMonth.SelectedValue, Me.ddlDPMonth.SelectedValue, 1)

Me.ddlDPMonth.SelectedValue is the value I need converted.

TIA,
Suzanne
 
I had to create an enum and go from there...
This should work:
Code:
 Enum Month
        January = 1
        February = 2
        March = 3
        April = 4
        May = 5
        June = 6
        July = 7
        August = 8
        September = 9
        October = 10
        November = 11
        December = 12
    End Enum
    Public Shared Function strMonthToIntMonth(ByVal strMonth As String) As Integer
        Try
            Return CType([Enum].Parse(GetType(Month), strMonth.ToUpper), Integer)
        Catch
            Return Nothing
        End Try
    End Function



-The answer to your problem may not be the answer to your question.
 
Thanks for the quick reply. I figured that was an option - I was just hoping there was a less wordy way :)

 
not really, thats why i pasted my code, it was more a pain than I had hoped.

I actually find it useful, because I had to make a enum for month abbreviations and a similar convert function, for the abbr and the int value.
I looked for like 2 days and couldn't find anything.

-The answer to your problem may not be the answer to your question.
 
Yuck. Thanks for your help.

You'd think something so simple wouldn't be so wordy! :)
 
actually its not that bad. would you rather do it old school:

case "January"
return 1
case "February"
return 2
case "March"
return 3

...you see where i am going with that...

-The answer to your problem may not be the answer to your question.
 
...and before you say that is the coolest way of doing things since sliced bread, think of if you ever need to have the month names in a drop down, its a real quick enum to array list function.

-The answer to your problem may not be the answer to your question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top