formatting Dates?

  • Thread starter Thread starter JD
  • Start date Start date
J

JD

I would like to know what is the preferred/best way to
format date values in VB.Net. Here is what I have

Dim str1 As String
str1 = txtDate.Text 'say 2/3/2005

I need 2/3/2005 to be 03FEB2005. Here is what I tried

str1 = txtDate.Text.ToString("ddMMMyyyy")

but I got an error message about System.IFormatProvider
something missing. I tried Imports System, still error.
So I then tried

str1 = Format(txtDate.Text, "ddMMMyyy")

which did work, and also tried

str1 = String.Format("ddMMMyyyy", txtDate.Text)

which also worked. But I know I have seen the
txtDate.Text.ToString("ddMMMyyyy") method somewhere. So
could someone suggest what is the preferred method to
format dates in VB.Net?

Thanks,
JD
 
The format codes you are using apply to a DateTime variables, not string
variables. You need to get the date in question into a DateTime variable.
Try something like:
Dim d As DateTime = Now
Dim s As String = d.ToString("ddMMMyyyy")
 
JD said:
I would like to know what is the preferred/best way to
format date values in VB.Net. Here is what I have

Dim str1 As String
str1 = txtDate.Text 'say 2/3/2005

I need 2/3/2005 to be 03FEB2005. Here is what I tried

str1 = txtDate.Text.ToString("ddMMMyyyy")

but I got an error message about System.IFormatProvider
something missing. I tried Imports System, still error.

You will have to convert the string to a 'Date' first:

\\\
Dim d As Date = DateTime.Parse("22/02/1999")
MsgBox(d.ToString("ddMMMyyyy"))
///
 
Thanks for everyone's help. That is what I was missing -
it was from the date variable, not the string.
 
Back
Top