Home All Groups Group Topic Archive Search About
Author
2 Apr 2005 8:00 PM
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

Author
2 Apr 2005 8:21 PM
AMercer
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")


Show quoteHide quote
"JD" wrote:

> 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
>
Author
2 Apr 2005 8:21 PM
Herfried K. Wagner [MVP]
"JD" <anonym***@discussions.microsoft.com> schrieb:
>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"))
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 Apr 2005 8:44 PM
JD
Thanks for everyone's help.  That is what I was missing -
it was from the date variable, not the string.



Show quoteHide quote
>-----Original Message-----
>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
>.
>