Q: Date to String

G

Geoff Jones

Hi

I've just found something very strange when using Date types within string.
Consider the following:

Dim myDate = DateValueRow("The Date")

' myDate is a date taken from a DataTable
' During debugging in a watch window it had value 5/28/03

Dim newStringDate = "The Date is " + myDate

' The value of newStringDate is now 28/05/03 i.e. the month and day have
swapped.

Can anybody explain why this is happening and how to avoid it?

Thanks in advance

Geoff
 
M

Marina

I am guessing that you have your PC set to a different date format then
american? Perhaps the debugger is not recognizing that, but the ToString
method of the datetime class does.

Also, you should turn option strict on, which will force all your variables
to have types, and not allow you to just add together variables of different
types.
 
C

Cor Ligthert

Hi Marina,

For your information.
I am guessing that you have your PC set to a different date format then
american? Perhaps the debugger is not recognizing that, but the ToString
method of the datetime class does.

The English VS.studio gives USA formatted dates in the debugger independent
from the region settings. (I do not know what the other language ones do
maybe Herfried can tell).

Cor


..
 
H

Herfried K. Wagner [MVP]

* "Geoff Jones said:
I've just found something very strange when using Date types within string.
Consider the following:

Dim myDate = DateValueRow("The Date")

' myDate is a date taken from a DataTable
' During debugging in a watch window it had value 5/28/03

Dim newStringDate = "The Date is " + myDate

' The value of newStringDate is now 28/05/03 i.e. the month and day have
swapped.

Can anybody explain why this is happening and how to avoid it?

Inside the IDE, the tooltips and auto window will always show the date
in US date/time format. In your code, 'myDate' is implycitly converted
to a string by concatenating it to a string. This conversion will be
done according to your system's date/time format settings.
 
G

Geoff Jones

Hi Marina

I switched on Strict as you suggested. I have also worked out a solution to
my problem; although I still don't understand exactly what is going on. By
the way, I had to change the date because otherwise I was getting an
exception that the date was not in the correct format.

Here is my new code:

Dim myDate As DateTime = CDate(DateValueRow("The Date"))

Dim newStringDate As String = "The Date is " + Format(myDate, "MM/dd/yy")

If I don't use Format, and use

Dim newStringDate As String = "The Date is " + myDate.ToString()

I still get the error message that the date is in the wrong format i.e.
inspection of the dates, even by Writeline, shows that the day and month
numbers are swapped in the two different codes.

Any further help would be most appreciated.

Geoff
 
C

Cor Ligthert

Hi Herfried,

Did you use the German VS version for this, I was curious about that?

Cor
 
S

SA

All:

What can also be the case (because we don't know which type of app the user
is debugging) is that somehow the Culture for the thread was set to a
European culture.

This could very well be the case in a .NET app, where someone else might
have written that code in the global.asax and Geoff isn't aware of that.
 

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