To display month like 08.

danyeungw

Well-known member
Joined
Aug 30, 2005
Messages
73
Programming Experience
10+
I wanted to get the month display as 08. Here is my code. I got "Arguments out of range exception" error on the second line. What was wrong?


Me.TextBoxDateIn.Text = DateTime.Today.ToShortDateString.ToString()

Me.TextBoxDateOut.Text = ("0" & CDate(Me.TextBoxDateIn.Text).Month.ToString).Substring(2, 2)


Actually, I need to have date like '08/24/2006' and 'August 24, 2006'. What is the best way?

Thanks.
DanYeung
 
Last edited:
Hi

You can use the String.Format method to format a date, like so:

Me.TextBoxDateOut.Text = string.Format("Todays date is: {0:MM\/dd\/yyyy}", now)

'this will show "08/24/2006"

use String.Format("{0:MMMM\/dd\/yyyy}", now)
"this will display "August/24/2006"

Have fun

G.
 
Last edited:
It works only the month. It showed 08/DD/YYYY and Auguster/DD/YYYY. Are they back slash and forward slash \/ ?

Thanks.
DanYeung
 
Yes, this are a backslash "\" followed by a slash "/".

the slash is not allowed in the string.format method, so you need the backslash as an escape.

String.Format("{0:MM\/dd\/yyyy}", Now)

however, if you want to use a different separator, e.g. a "," you can use

String.Format("{0:MM,dd,yyyy}", Now)

try it

G.
 
This is VB. The ONLY character that needs escaping in strings is the double quote. If you want today's date in the formats you specified use either of:
VB.NET:
MessageBox.Show(Date.Today.ToString("dd/MM/yyyy"))
MessageBox.Show(Date.Today.ToString("MMMM d, yyyy"))

MessageBox.Show(String.Format("{0:dd/MM/yyyy}", Date.Today))
MessageBox.Show(String.Format("{0:MMMM d, yyyy}", Date.Today))
Obviously the first option is cleaner, but String.Format is useful if you want to insert the date string into a bigger string, particularly if you have other formatted values to insert as well.
 
Back
Top