views:

32

answers:

3

For example:

Dim testdate As String = "29/10/2010"
testdate = Convert.ToDateTime(testdate.ToString)
Response.Write(testdate)

expecting "29/10/2010 00:00:00" what I get is "29/10/2010"

+3  A: 

You have to assign the result of Convert.ToDateTime to a DateTime object, not to the string.

Dim testdate As String = "29/10/2010"
Dim date As DateTime = Convert.ToDateTime(testdate)
Response.Write(date)

This will print the clock as well as the date in the default format for your machine.

Mikael Svenson
A: 

As clearly documented here, no, this is not the designed behaviour.
  '05/01/1996' converts to 5/1/1996 12:00:00 AM.
  'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.

It's possible that this article might provide insight.

Eric Towers
Returning am/pm or 24h depends on the locale/culture of your machine, unless you specify it in your program. So the default output of date.ToString() is very individual.
Mikael Svenson
my site is set to en-GB in web.config, but the database server where the info needs to go is set to en-US
Phil
+1  A: 

The big problem is, as mentioned in other posts, you are implicitly assigning the DateTime result of the Convert operation to a string variable. This wouldn't even pass the compilier in C#, so your into the realm of how VB operates on these implicit assignments and you probably don't want to be there because if some part of the language specification changes in a new Framework version and you try to migrate your code, you could potentially have a nasty bug to try to find (maybe not as important here, but in other cases, yes). Best move would be to rewrite the code block to have the Convert operation assign to a DateTime object, but simplier solution would be to throw a .ToString() at the end of the Convert.ToDateTime(testdate) line (i.e. testdate = Convert.DateTime(testdate).ToString() which will leave testdate with the date and the timestamp formatted to the current culture (as you are now performing an explicit conversion between the DateTime and the destination String).

ben f.