views:

112

answers:

1

I have a string datetime in this format "dd-MM-yyyy HH:mm:ss.fff"

like so..

DateTime mydate = Convert.ToDateTime("13-09-2010 02:30:14.905"); 
 result.SetValue(1, mydate);

//I get a error saying 'String was not recognized as a valid DateTime'

if i do it this way

DateTime mydate = DateTime.ParseExact("13-09-2010 02:30:14.905", "dd-MM-yyyy HH:mm:ss.fff", CultureInfo.CurrentCulture);
 result.SetValue(1, mydate);

it works fine but formats it without the milliseconds.

i just need the datetime object to hold my formatted time so i can insert it.

+2  A: 

DateTime doesn't hold a "formatted time" - it just holds a time. It's like int doesn't hold a "hex value" or a "decimal value" - it just holds an integer.

Your second version is almost certainly parsing all the data correctly - print it out to check - so it's the database interaction that's causing the problems, I suspect. You can isolate the parsing from the database interaction by explicitly constructing a DateTime value for a test program, of course.

You should be aware that according to the SQL Server CE 3.5 docs, the datetime type in SQL Server CE only supports a granularity of one three-hundredth of a second, i.e. just over 3ms. If you need an exact millisecond value, you may want to consider an alternative representation (e.g. a datetime accurate to the second, and a separate integer field for milliseconds.)

Jon Skeet
well its just a datetime column. for some reason the second version is excluding the milliseconds before it is even inserted. it adds slashes and formats it like this 9/13/2010 3:12:22 AM if i convert it back to a string
damnitall
Make sure that when you convert it back to string, you specify a custom format with ".fff" for milliseconds. The default DateTime.ToString does not include them AFAIK.
liggett78
ok i think i understand it now. if it doesnt sort right i will just store the damn time as a int and sort it that way,
damnitall
thanks for the help
damnitall