469,950 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Convert a date string

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.

Dec 15 '06 #1
9 12699
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:
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.
Dec 15 '06 #2
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:
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.
Dec 15 '06 #3

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.

Dec 15 '06 #4
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.
Dec 15 '06 #5
"rowe_newsgroups" <ro********@yahoo.comha 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.

Dec 15 '06 #6
Thank you, eveybody.
Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it.

Thanks.
Fabio Z wrote:
"rowe_newsgroups" <ro********@yahoo.comha 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.
Dec 15 '06 #7
fpvt,

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

Cor

<fp***@yahoo.comschreef in bericht
news:11**********************@j72g2000cwa.googlegr oups.com...
Thank you, eveybody.
Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it.

Thanks.
Fabio Z wrote:
>"rowe_newsgroups" <ro********@yahoo.comha 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.

Dec 15 '06 #8

"Cor Ligthert [MVP]" <no************@planet.nlha scritto nel messaggio
news:uE*************@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).

Dec 16 '06 #9
Fabio,

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

I was missing the date completely.,

:-)

Cor

"Fabio" <zn*******@virgilio.itschreef in bericht
news:OF**************@TK2MSFTNGP04.phx.gbl...
>
"Cor Ligthert [MVP]" <no************@planet.nlha scritto nel messaggio
news:uE*************@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).

Dec 16 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Hector A | last post: by
2 posts views Thread by Franck | last post: by
8 posts views Thread by deepak_kamath_n | last post: by
3 posts views Thread by Jef Driesen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.