DateTime.ToShortDateString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将当前 DateTime 对象的值转换为其等效的短日期字符串表示形式。
public:
System::String ^ ToShortDateString();
public string ToShortDateString ();
member this.ToShortDateString : unit -> string
Public Function ToShortDateString () As String
返回
一个字符串,它包含当前 DateTime 对象的短日期字符串表示形式。
示例
下面的示例演示 ToShortDateString 方法。 它还显示调用方法的结果与 ToShortDateString DateTime.ToString(String) 通过将 "d" 作为格式参数调用方法相同。
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 与当前区域性关联的属性所定义的模式来设置当前对象的值的格式。 返回值与通过使用方法指定 "d" 标准 DateTime 格式字符串 返回的值相同 ToString(String) 。
备注
方法返回的字符串 ToShortDateString 是区分区域性的。 它反映由当前区域性的属性定义的模式 DateTimeFormatInfo.ShortDatePattern 。 例如,对于 en-us 区域性,标准短日期模式为 "M/d/yyyy";对于取消的区域性,它是 "dd. MM. yyyy";对于 ja-jp 区域性,它为 "yyyy/MM/dd"。 请注意,根据 .NET 实现及其版本、操作系统及其版本和用户自定义情况,其值会有所不同。
有关当前线程区域性的详细信息,请参阅 CultureInfo.CurrentCulture 和 Thread.CurrentCulture 属性。 有关格式字符、格式模式以及它们生成的输出的详细信息,请参阅标准日期和时间格式字符串和自定义日期和时间格式字符串。 有关更改与格式字符相关的格式模式的详细信息,请参阅 DateTimeFormatInfo 类。