views:

62

answers:

3

Hello, I have a variable for startdate as shown below.

Dim startDate as Datetime

startDate = Convert.ToDateTime(datepicker1.text).ToString("dd-MMM-yyyy")

In the immediate window if I try Convert.toDate(datepicker1.text).toString("dd-MMM-yyyy") it is showing date as 29-Sep-2009 but the value of startDate is 09/29/2009

I really don't understand why the value is not set in startDate variable as 29-Sep-2009

Any thoughts?

+2  A: 

You are simply seeing a different representation of the same date - your code is working just fine.

Your debug window is showing a string representation of the DateTime struct by calling its ToString method without any arguments (this is how all the debug windows get string representations of any object) which is simply displaying differently.

To see this in action - try this in the immediate window instead:

Convert.ToDateTime(datepicker1.text).ToString()

and you will see the same result that you are seeing in your watch window.

The important thing to remember is that a DateTime is an abstract concept that can have many different representations. It is also important to remember that just because you are seeing different representations of the same data it doesn't change the underlying type itself.

Andrew Hare
A: 

Try this instead:

Dim startDate as Datetime

startDate = DateTime.Parse(datepicker1.text);

You're using a different date format than what is used in the US, so it might be necessary to pass in an IFormatProvider instance as the second parameter to the DateTime.Parse call.

David Andres
A: 

Datetime variables are stored as the number of 100-nanosecond periods since 12:00 midnight, January 1, 0001 A.D. When you assign a value of 29-Sep-2009, it is converted to a single number.

There are lots of ways to represent this number. ToString("dd-MMM-yyyy") specifies one format (29-Sep-2009) and your system has some default formats, one of which is 09/29/2009.

It's good to keep in mind that if you use the default system format for the date, different users may see different date formats depending on their location and configuration.

xpda