Convert Month number to a string of the Month name
Posted: 12/9/2004 10:53:29 PM
By: Comfortably Anonymous
Times Read: 2,712
0 Dislikes: 0
Topic: Programming: .NET Framework
In case you need to convert an integer from 1 to 12 to a string containing the corresponding month, use this function. Pass it an int from 1 to 12...

(I haven't added any sanity-checking code - If you pass it anything outside that range, it will return a null string rather than throwing an exception. If you need that, just add a "Case Default" line at the bottom of the function...)

    Private Function MonthToString(ByVal month As Integer) As String
        Select Case month
            Case 1
                Return "January"
            Case 2
                Return "February"
            Case 3
                Return "March"
            Case 4
                Return "April"
            Case 5
                Return "May"
            Case 6
                Return "June"
            Case 7
                Return "July"
            Case 8
                Return "August"
            Case 9
                Return "September"
            Case 10
                Return "October"
            Case 11
                Return "November"
            Case 12
                Return "December"
        End Select
    End Function
Rating: (You must be logged in to vote)
Discussion View:
Replies:

Convert Month number to a string of the Month name
Posted: 12/9/2004 10:53:29 PM
By: Comfortably Anonymous
Times Read: 2,712
0 Dislikes: 0
Topic: Programming: .NET Framework
OK, that was pretty stupid of me. Just found out that there is a built-in VB.Net function called MonthName() that does the exact same thing and even supports abbreviations... Duh...

Returns a String value containing the name of the specified month.

Public Function MonthName( _
   ByVal Month As Integer, _
   Optional ByVal Abbreviate As Boolean = False _
) As String

Parameters:

- Month
Required. Integer. The numeric designation of the month, from 1 through 13; 1 indicates January and 12 indicates December. You can use the value 13 with a 13-month calendar. If your system is using a 12-month calendar and Month is 13, MonthName returns an empty string.

- Abbreviate
Optional. Boolean value that indicates if the month name is to be abbreviated. If omitted, the default is False, which means the month name is not abbreviated.
Rating: (You must be logged in to vote)