EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.

DateTime.ToShortDateString Method

May 11, 2014

Converts the value of the current DateTime object to its equivalent short date string representation.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public string ToShortDateString()

Return Value

Type: System.String
The short date string representation of the current DateTime object.

The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. The return value is identical to the value returned by specifying the "d" standard date and time format string with the ToString(String) method.

Important noteImportant Note:

The string returned by the ToShortDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo object. For example, for the en-US culture, the standard short date pattern is "M/d/yyyy"; for the de-DE culture, it is "dd.MM.yyyy"; for the ja-JP culture, it is "yyyy/M/d". The specific format string on a particular computer can also be customized so that it differs from the standard short date format string.

For more information about changing the format pattern associated with a format character, see the DateTimeFormatInfo class.

The following example demonstrates the ToShortDateString method. It also shows that the result of calling the ToShortDateString method is identical to calling the DateTime.ToString(String) method with "d" as the format parameter.


Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dateToDisplay As Date = #6/1/2009 8:42:50 AM#
      Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      ' Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      outputBlock.Text += String.Format("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name) & vbCrLf
      outputBlock.Text += String.Format("   {0} (Short Date String) & vbCrLf", _
                        dateToDisplay.ToShortDateString())
      ' Display using 'd' standard format specifier to illustrate it is
      ' identical to the string returned by ToShortDateString.
      outputBlock.Text += String.Format("   {0} ('d' standard format specifier) & vbCrLf", _
                        dateToDisplay.ToString("d"))
      outputBlock.Text &= vbCrLf

      ' Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      outputBlock.Text += String.Format("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name) & vbCrLf
      outputBlock.Text += String.Format("   {0}", dateToDisplay.ToShortDateString()) & vbCrLf
      outputBlock.Text &= vbCrLf

      ' Change culture to nl-NL.    
      Thread.CurrentThread.CurrentCulture = New CultureInfo("nl-NL")
      outputBlock.Text += String.Format("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name) & vbCrLf
      outputBlock.Text += String.Format("   {0}", dateToDisplay.ToShortDateString()) & vbCrLf

      ' Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture
   End Sub
End Module
' The example displays the following output:
'       Displaying short date for en-US culture:
'          6/1/2009 (Short Date String)
'          6/1/2009 ('d' standard format specifier)
'       
'       Displaying short date for fr-FR culture:
'          01/06/2009
'       
'       Displaying short date for nl-NL culture:
'          1-6-2009


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Afficher:
© 2014 Microsoft