Converting String to A Date

C

c_shah

I have a string in this format 100106 what is the most efficient way
to convert it to a date like 10/01/2006 thanks.
 
H

Herfried K. Wagner [MVP]

c_shah said:
I have a string in this format 100106 what is the most efficient way
to convert it to a date like 10/01/2006 thanks.

Take a look at 'Date.ParseExact'.
 
C

Cor Ligthert [MVP]

Take a look at 'Date.ParseExact'.

--
I would never advice that especially when I don't know if it has to be 10
januari 2006 or october 1 2006.

The CDate would be the nearest for me as xamman already wrote,

Cor
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
I would never advice that especially when I don't know if it has to be 10
januari 2006 or october 1 2006.

The CDate would be the nearest for me as xamman already wrote,

Cor

I would never advice that, as the date format clearly needs to be
specified exactly. Just converting it to a date without an exact format
is not likely to produce a usable result, and the result may even vary
depending on culture settings.

With ParseExact you specify exactly how the date format is supposed to
be interpreted. The OP of course knows if the format is "ddMMyy" or
"MMddyy", otherwise parsing the value is impossible.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
I would never advice that especially when I don't know if it has to be 10
januari 2006 or october 1 2006.

The OP provided a sample and another sample showing how the date should be
parsed. This can be archieved by 'Time.ParseExact' with a fixed format
string, but---as Göran mentions---cannot be archieved using 'CDate'.
 

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