Using an array to display Month name, September etc ..

simonwar

Member
Joined
Sep 8, 2009
Messages
17
Programming Experience
Beginner
Hi,

I am tuning my knowlegde of Date and Times, but why doesn't this work

VB.NET:
        Dim now1=DateAndTime.Now
        Dim month As Integer = now1.Month

        'creates an array to determine the month name
        Dim montharray() As String = {"January", "February", "March", "April", "May", "June", "July", "August", "September, "October", "November", "December"}
        TextBox1.Text = montharray(month)

also, using the array, I noticed after 10 entries the array was looking to close, highlighting October, November and December as errors.

No major deal, but annoying nevertheless as I am trying to learn VB.net

Thanks, Simon.
 
VB.NET:
MessageBox.Show(Date.Now.ToString("MMMM"))

Whilst you may need arrays at some point, investigate List (of T) - which is normally far more useful.
 
Arrays start with a zero based item.
montharray(0) would equal January
montharray(1) would equal Feb etc

As posted above there are easier ways of getting a displaying a month without the need of creating an array but for the puroses of demonstrating your problem, put the following code into a console app's sub main to take a look at your arrays outputted results.

VB.NET:
    Sub Main()

        Dim datNow1 = DateAndTime.Now
        Dim intMonth As Integer = datNow1.Month
        Dim arrMonths() As String = { _
                        "January", "February", "March", "April", _
                        "May", "June", "July", "August", "September", _
                        "October", "November", "December"}

        Console.WriteLine("#################################")
        Console.WriteLine()
        Console.WriteLine(" Original Coding")
        Console.WriteLine()
        Console.WriteLine("now1 : {0}", datNow1)
        Console.WriteLine("intMonth : {0}", intMonth)
        Console.WriteLine("arrMonths({0}) : {1}", intMonth, arrMonths(intMonth))
        Console.WriteLine()
        Console.WriteLine("#################################")
        Console.WriteLine()

        For intIndex As Integer = 0 To (arrMonths.Count - 1)

            Console.WriteLine("Array Index {0}) = {1}" _
                              , Format(intIndex, "00") _
                              , arrMonths(intIndex))
        Next intIndex

        Console.ReadLine()

    End Sub
 
I was going to give you the code, but as you say
I am tuning my knowledge of Date and Times
Try working with
VB.NET:
Microsoft.VisualBasic.MonthName()
Which returns the name of the month based on an integer value, You can use a For Loop to add months to an array e.g:
VB.NET:
arrayMonths(i)=Microsoft.VisualBasic.MonthName(i)
I wish you the best of luck in your learning. N.B There are many useful functions under the Microsoft.VisualBasic Namespace for working with dates, i suggest you experiment with them.
 
Last edited:
Ive always used .ToString("MMMM")

VB.NET:
Dim now1 as datetime = now
TextBox1.text = now1.tostring("MMMM")
 
To simplify it even further, there really is no need to even create the datetime variable

VB.NET:
TextBox1.Text = Now.ToString("MMMM")
 
Or you could say :
VB.NET:
  Dim month As Interger = Now.Month
TextBox1.Text=MonthName(month)
'MonthName being a function in the Microsoft.VisualBasic Namespace
It would work fine also.
 
If you want to get the name of the month from a specific date then I would always use ToString("MMMM"). It's the shortest and most consistent and no other other option offers any advantage.

If you actually want an array containing month names then you can get one from the DateTimeFormatInfo.MonthNames property.
 
Back
Top