Convert a date string

F

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.
 
R

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
 
R

rowe_newsgroups

I'm thinking he's using hungarian notation - so sdate is probably
declared as a string, not a datetime.

Thanks,

Seth Rowe
 
B

Branco Medeiros

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.
 
F

Fabio Z

rowe_newsgroups said:
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.
 
P

Phill W.

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.
 
F

fpvt2

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

Thanks.
 
F

Fabio

Cor Ligthert said:
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).
 
C

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top