Home All Groups Group Topic Archive Search About

Convert a date string

Author
15 Dec 2006 3:57 PM
fpvt2
In VB6, I can format a date string like the following:
format("12/15/06","yymmdd") and it returns 061215.
In VB.Net, when I do the following:
sdate.tostring("yymmdd"), it gave me an error "Value of type string
cannot be converted to 'System.IFormatProvider'

How can I do it in VB.NET ?
Thanks.

Author
15 Dec 2006 4:08 PM
Kerry Moorman
fpvt2,

I did not have any problems getting the following code to work:

            Dim d1 As DateTime = Now

            Console.WriteLine(d1.ToString("yyMMdd"))

Maybe you can post the exact code that is giving you a problem?

Kerry Moorman


Show quoteHide quote
"fp***@yahoo.com" wrote:

> In VB6, I can format a date string like the following:
> format("12/15/06","yymmdd") and it returns 061215.
> In VB.Net, when I do the following:
> sdate.tostring("yymmdd"), it gave me an error "Value of type string
> cannot be converted to 'System.IFormatProvider'
>
> How can I do it in VB.NET ?
> Thanks.
>
>
Author
15 Dec 2006 4:11 PM
rowe_newsgroups
I'm thinking he's using hungarian notation - so sdate is probably
declared as a string, not a datetime.

Thanks,

Seth Rowe


Kerry Moorman wrote:
Show quoteHide quote
> fpvt2,
>
> I did not have any problems getting the following code to work:
>
>             Dim d1 As DateTime = Now
>
>             Console.WriteLine(d1.ToString("yyMMdd"))
>
> Maybe you can post the exact code that is giving you a problem?
>
> Kerry Moorman
>
>
> "fp***@yahoo.com" wrote:
>
> > In VB6, I can format a date string like the following:
> > format("12/15/06","yymmdd") and it returns 061215.
> > In VB.Net, when I do the following:
> > sdate.tostring("yymmdd"), it gave me an error "Value of type string
> > cannot be converted to 'System.IFormatProvider'
> >
> > How can I do it in VB.NET ?
> > Thanks.
> >
> >
Author
15 Dec 2006 4:09 PM
rowe_newsgroups
AFAIK the .ToString(...dateformat...) only works with a DateTime
object. Try this instead:

MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd"))

Thanks,

Seth Rowe


fp***@yahoo.com wrote:
Show quoteHide quote
> In VB6, I can format a date string like the following:
> format("12/15/06","yymmdd") and it returns 061215.
> In VB.Net, when I do the following:
> sdate.tostring("yymmdd"), it gave me an error "Value of type string
> cannot be converted to 'System.IFormatProvider'
>
> How can I do it in VB.NET ?
> Thanks.
Author
15 Dec 2006 4:21 PM
Fabio Z
"rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio

> AFAIK the .ToString(...dateformat...) only works with a DateTime
> object. Try this instead:
>
> MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd"))

Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day
or the month.
Author
15 Dec 2006 4:28 PM
fpvt2
Thank you, eveybody.
Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it.

Thanks.


Fabio Z wrote:
Show quoteHide quote
> "rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio
>
> > AFAIK the .ToString(...dateformat...) only works with a DateTime
> > object. Try this instead:
> >
> > MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd"))
>
> Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day
> or the month.
Author
15 Dec 2006 8:17 PM
Cor Ligthert [MVP]
fpvt,

Or in true VisualBasic
CDate("12/15/06").ToString("yyMMdd")

Cor

<fp***@yahoo.com> schreef in bericht
Show quoteHide quote
news:1166200114.018476.103570@j72g2000cwa.googlegroups.com...
> Thank you, eveybody.
> Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it.
>
> Thanks.
>
>
> Fabio Z wrote:
>> "rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio
>>
>> > AFAIK the .ToString(...dateformat...) only works with a DateTime
>> > object. Try this instead:
>> >
>> > MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd"))
>>
>> Pay attention with dates as "1/5/2006" where you can't tell if 1 is the
>> day
>> or the month.
>
Author
16 Dec 2006 8:50 PM
Fabio
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:uEOE2WIIHHA.924@TK2MSFTNGP02.phx.gbl...
> fpvt,
>
> Or in true VisualBasic
> CDate("12/15/06").ToString("yyMMdd")
>

I repeat: watch out for locale dates.
I don't think that the date "12/15/06" is hard coded.
I.e. in my country (Italy) that date does not exists, because 15 would be
the month (dd/mm/yyyy).
Author
16 Dec 2006 9:47 PM
Cor Ligthert [MVP]
Fabio,

Normally I am the one who is writting as you did in the dotNet newsgroup.

I was missing the date completely.,

:-)

Cor

Show quoteHide quote
"Fabio" <znt.fa***@virgilio.it> schreef in bericht
news:OFBpWPVIHHA.1064@TK2MSFTNGP04.phx.gbl...
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> ha scritto nel messaggio
> news:uEOE2WIIHHA.924@TK2MSFTNGP02.phx.gbl...
>> fpvt,
>>
>> Or in true VisualBasic
>> CDate("12/15/06").ToString("yyMMdd")
>>
>
> I repeat: watch out for locale dates.
> I don't think that the date "12/15/06" is hard coded.
> I.e. in my country (Italy) that date does not exists, because 15 would be
> the month (dd/mm/yyyy).
>
>
>
Author
15 Dec 2006 4:18 PM
Branco Medeiros
fp***@yahoo.com wrote:
> In VB6, I can format a date string like the following:
> format("12/15/06","yymmdd") and it returns 061215.
> In VB.Net, when I do the following:
> sdate.tostring("yymmdd"), it gave me an error "Value of type string
> cannot be converted to 'System.IFormatProvider'
>
> How can I do it in VB.NET ?

I guess you must convert the string to date first (something VB6 did
for you, automagically), and *then* format the date the way you want.
=)

I mean:

  Dim D As String = _
    Date.ParseExact(sDate, "mm/dd/yy", Nothing).ToString("yymmdd")

HTH

Regards,

Branco.
Author
15 Dec 2006 4:24 PM
Phill W.
fp***@yahoo.com wrote:
> In VB6, I can format a date string like the following:
> format("12/15/06","yymmdd") and it returns 061215.
> In VB.Net, when I do the following:
> sdate.tostring("yymmdd"), it gave me an error "Value of type string
> cannot be converted to 'System.IFormatProvider'

That's because VB 'Proper's Evil Type Coersion implicitly changed the
string value "12/15/06" into a Date value and then formatted /that/.

Visual Basic forces you to take more care over how things do things.
Formatting (ToString'ing) a String is very different from the formatting
a Date:

? DateTime.Parse( "12/15/06").ToString( "yymmdd" )

Or, since you start and end with Strings:

sDate.Substring( 6, 2 ) _
    & sDate.Substring( 0, 2 ) _
    & sData.Substring( 3, 2 )

HTH,
    Phill  W.