'Format' method to convert date format

T

thomasc1020

This is regarding VB.NET 2003.

Variable 'Date' is a string and it contains date information in this
format: "DEC/05/2007".
Now I am trying to convert the format as "2007-12-05".

Is it possible to convert the arrangement using 'Format' method?
If so, please educate me how I can do such operation.

Thank you!
 
P

Phill W.

Variable 'Date' is a string ...

.... which is a /lousy/ name for a String variable, BTW ...
(Hang on! Isn't "Date" [still] a reserved word??)
... and it contains date information in this format:
"DEC/05/2007".
Now I am trying to convert the format as "2007-12-05".

? DateTime.ParseExact( "MMM/dd/yyyy" ).ToString( "yyyy-MM-dd" )

will work, so long as your in an English or French-speaking Locale; you
/might/ have problems elsewhere in the World.
Is it possible to convert the arrangement using 'Format' method?

Not directly.
Format() will take a DateTime variable and convert it into a String but
it can't take a String (in a non-standard date format) and do anything
useful with it.


Just out of curiosity, where did you get the data in that format
(MMM/dd/yyyy") /from/? I don't think I've ever seen that one before.

HTH,
Phill W.
 
S

SurturZ

A straight string manipulation will do it, if you aren't doing any date
arithmetic.

Just use the Mid() function and translate the month names to a numeral.

If you are doing date arithmetic, then use the DateSerial() function after
converting the month name.
 
S

Stephany Young

DateTime.ParseExact(StrConv([Date], VbStrConv.ProperCase), "MMM/dd/yyyy",
CultureInfo.InvariantCulture).ToString("yyyy-MM-dd")

Note the use of StrConv([Date], VbStrConv.ProperCase) to change DEC to Dec,
otherwise the DateTime.ParseExact won't work correctly.
 

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