Date Format Problem

前端 未结 1 962
后悔当初
后悔当初 2020-12-22 05:49

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 dateStrin         


        
相关标签:
1条回答
  • 2020-12-22 06:54

    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.

    0 讨论(0)
提交回复
热议问题