tags:

views:

148

answers:

1

Hi,

i cant change date value to the format which i want.This is my code.When i execute this code,i see mydate as mm/DD/yyyy format.what do i miss?

 Dim dateString As String = Date.Now.ToString("dd.MM.yyyy")    
 Dim myDate As Date = Date.ParseExact(dateString, "dd.MM.yyyy", Nothing)
+8  A: 

You don't miss anything. myDate is a Date which internally stores the date as a numerical form (as an UInt64 to be more precise). It does not store the string representation of the date, and it has no format in itself. When it is displayed as a string (for instance using the ToString method), if no date format is specified, it will use the default format for the current culture, which in your case seems to be MM/dd/yyyy.

Update

I will try to make this a bit more clear.

What you are experiencing is actually a rather common misunderstanding. You really need to make a difference between a date and the string representation of a date. These are two different things.

The Date (which is acutally a DateTime with no time information, so I will refer to DateTime here) stores the date and time internally as a number. Currently, where I am, this number is 9857259848526375120. That is the current date and time, as represented inside a DateTime object.

While this value is very useful to the code, it makes little sense for us humans. We have complicated the matter a bit more by deciding to write dates in different ways in different countries. So, here in Sweden, todays date would be written as "2009-09-17", while in Turkey it would be "17.09.2009".

So, in order to make things easier for us, whenever a DateTime object is displayed as text (in the VS debugger, when printed on screen and so on), the numerical value is formatted into a string using some date format. This format will be different in different countries. If no format is specified when displaying the string, the default format for the current culture will be used. This is the case in the VS watch window for instance.

If you wish to get the string representation of the DateTime object formatted according to a specific culture, you can pass it to the ToString method as such:

myDate.ToString(CultureInfo.GetCultureInfo("tr-TR"))

If you, on the other hand, need to convert the date into some other, specific format, you can use a format string to achieve that:

Dim formattedDate1 As String = myDate.ToString("dd'/'MM'/'yyyy")
Dim formattedDate2 As String = myDate.ToString("dd.MM.yyyy")

Note the single quotes around the / characters in the format string; they are needed since the system will by default interpret the / as a placeholder for the current culture's date separator. If you wish to force the formatted date to use a / character rather than the normal separator for the current culture ('-' in Sweden, '.' in Turkey), you need to escape it as in my example.

Fredrik Mörk
Hi, Although i get culture info and use it,it doesnt change.i try this Dim myCultureInfo As New System.Globalization.CultureInfo("tr-TR") Dim myDate As Date = Date.ParseExact(dateString, "dd.MM.yyyy", myCultureInfo)
Alexander
How are you "seeing" mydate? In a Visual Studio watch window or from your program output? If the latter how are you creating the output?
MarkJ
You missed my point. The `Date` object that is returned by `ParseExact` *does not have any format*. It's just a number. In order to make it understandable for us mere humans it is converted into a comprehensible string representation. That is when the format comes into the picture. Try to add this code: `Dim temp As String = myDate.ToString(CultureInfo.GetCultureInfo("tr-TR"))`. That will take the `Date` object and convert it's internal numerical representation of the date into a string, formatted according to Turkish date formatting rules.
Fredrik Mörk
ok fredrik,i got it.But i wonder that forexample if i want to send a date parameter to query,can oracle TO_DATE function be enough to change my format.I am using date's methods in my function and i cant use string,in my database dates store in dd/mm/yyyy format.If i use string,i dont come across problem but can date type cause problem in this case?
Alexander
@Cem: added some clarification to my answer. I hope it contains what you need.
Fredrik Mörk
@Fredrik:Sorry for my late response.I would like to thank ur explanation but my problem was different.The problem was about my web config and when i add tr culture,it solved my problem.Thanks again.
Alexander