'Format' method to convert date format

  • Thread starter Thread starter thomasc1020
  • Start date Start date
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!
 
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.
 
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.
 
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.
 
Back
Top