DateTime.ToShortDateString DateTime.ToShortDateString DateTime.ToShortDateString DateTime.ToShortDateString Method

定义

将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。Converts the value of the current DateTime object to its equivalent short date string representation.

public:
 System::String ^ ToShortDateString();
public string ToShortDateString ();
member this.ToShortDateString : unit -> string
Public Function ToShortDateString () As String

返回

一个字符串,它包含当前 DateTime 对象的短日期字符串表示形式。A string that contains the short date string representation of the current DateTime object.

示例

下面的示例演示ToShortDateString方法。The following example demonstrates the ToShortDateString method. 它还表明,调用的结果ToShortDateString方法等同于调用DateTime.ToString(String)使用"d"作为 format 参数的方法。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.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      DateTime dateToDisplay = new DateTime(2009, 6, 1, 8, 42, 50);
      CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
      // Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0} (Short Date String)", 
                        dateToDisplay.ToShortDateString());
      // Display using 'd' standard format specifier to illustrate it is
      // identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", 
                        dateToDisplay.ToString("d"));
      Console.WriteLine();
      
      // Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      Console.WriteLine();
  
      // Change culture to nl-NL.    
      Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
      Console.WriteLine("Displaying short date for {0} culture:", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString());
      
      // Restore original culture.
      Thread.CurrentThread.CurrentCulture = originalCulture;
   }
}
// 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
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim dateToDisplay As Date = #06/01/2009 8:42:50#
      Dim originalCulture As CultureInfo = Thread.CurrentThread.CurrentCulture
      ' Change culture to en-US.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0} (Short Date String)", _
                        dateToDisplay.ToShortDateString())
      ' Display using 'd' standard format specifier to illustrate it is
      ' identical to the string returned by ToShortDateString.
      Console.WriteLine("   {0} ('d' standard format specifier)", _
                        dateToDisplay.ToString("d"))
      Console.WriteLine()
      
      ' Change culture to fr-FR.
      Thread.CurrentThread.CurrentCulture = New CultureInfo("fr-FR")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString())
      Console.WriteLine()
  
      ' Change culture to nl-NL.    
      Thread.CurrentThread.CurrentCulture = New CultureInfo("nl-NL")
      Console.WriteLine("Displaying short date for {0} culture:", _
                        Thread.CurrentThread.CurrentCulture.Name)
      Console.WriteLine("   {0}", dateToDisplay.ToShortDateString())
      
      ' 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

注解

当前值DateTime对象的格式使用定义的模式DateTimeFormatInfo.ShortDatePattern与当前线程区域性关联的属性。The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread culture. 返回值是通过指定"d"返回的值相同标准 DateTime 格式字符串ToString(String)方法。The return value is identical to the value returned by specifying the "d" standard DateTime format string with the ToString(String) method.

备注

返回的字符串ToShortDateString方法是区分区域性的。The string returned by the ToShortDateString method is culture-sensitive. 它反映当前区域性定义的模式DateTimeFormatInfo.ShortDatePattern属性。It reflects the pattern defined by the current culture's DateTimeFormatInfo.ShortDatePattern property. 例如,对于 EN-US 区域性,标准的短日期模式是"年-月-日";对于 DE-DE 区域性中,它是"dd。MM.yyyy";对于 JA-JP 区域性,它是"yyyy/MM/dd"。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/MM/dd". 请注意,其值可以有所不同的.NET 实现和它的版本、 操作系统和其版本和用户自定义。Note that its value can vary depending on the .NET implementation and its version, the operating system and its version, and user customization.

有关当前线程区域性的详细信息,请参阅 CultureInfo.CurrentCultureThread.CurrentCulture 属性。For more information about the current thread culture, see the CultureInfo.CurrentCulture and Thread.CurrentCulture properties. 有关格式字符、格式模式以及它们生成的输出的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For more information about format characters, format patterns, and the output they produce, see Standard date and time format strings and Custom date and time format strings. 有关更改与格式字符相关的格式模式的详细信息,请参阅 DateTimeFormatInfo 类。For more information about changing the format pattern associated with a format character, see the DateTimeFormatInfo class.

适用于

另请参阅