Initializing a DateTime variable with a string date

  • Thread starter Thread starter Gary Rynearson
  • Start date Start date
G

Gary Rynearson

I am looking to turn the string "20051211 14:22:33" into a dateTime variable
whose date is December 11, 2005 and time is 2:22:33 PM. I have tried
several approaches, I MUST be missing something simple. I have spent more
time on this than anyone should have to.

DateTime myDateTime = DateTime.Parse ("20051211 14:22:33")

I get a "Syste;m.FormatExcption: String was not recognized as a valid
DateTime" error.
 
Gary,

You can call DateTime.ParseExact and pass in a format string
describing what the date is formatted like.


Mattias
 
Mattias

I have tried this, but cannot come up with the correct string. This is
exactly the fustrating detail I am looking for help with.

Gary
 
Gary Rynearson said:
Mattias

I have tried this, but cannot come up with the correct string. This is
exactly the fustrating detail I am looking for help with.

Gary
....
try:
DateTime myDateTime = DateTime.ParseExact("20051211 14:22:33", "yyyyMMdd
HH:mm:ss", DateTimeFormatInfo.InvariantInfo);

Regards,
Goran
 
Goran

Thanks very much for you help. Your suggestion works exactly as I hoped.
Note: the DateTimeFormatInfo.InvariantInfo class / property requires "using
System.Globalization;"

Gary
 
Back
Top