DateTime.ToShortDateString 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將目前 DateTime 物件的值轉換為其相等的簡短日期 (Short Date) 字串表示。
public:
System::String ^ ToShortDateString();
public string ToShortDateString ();
member this.ToShortDateString : unit -> string
Public Function ToShortDateString () As String
傳回
字串,內含目前 DateTime 物件的簡短日期字串表示。
範例
下列範例示範 ToShortDateString 方法。 它也會顯示呼叫 ToShortDateString 方法的結果與使用 「d」 作為格式參數呼叫 DateTime.ToString(String) 方法的結果相同。
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
open System
open System.Globalization
open System.Threading
let dateToDisplay = DateTime(2009, 6, 1, 8, 42, 50)
let originalCulture = Thread.CurrentThread.CurrentCulture
// Change culture to en-US.
Thread.CurrentThread.CurrentCulture <- CultureInfo "en-US"
printfn "Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $" {dateToDisplay.ToShortDateString()} (Short Date String)"
// Display using 'd' standard format specifier to illustrate it is
// identical to the string returned by ToShortDateString.
printfn $" {dateToDisplay:d} ('d' standard format specifier)\n"
// Change culture to fr-FR.
Thread.CurrentThread.CurrentCulture <- CultureInfo "fr-FR"
printfn $"Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $" {dateToDisplay.ToShortDateString()}\n"
// Change culture to nl-NL.
Thread.CurrentThread.CurrentCulture <- CultureInfo "nl-NL"
printfn $"Displaying short date for {Thread.CurrentThread.CurrentCulture.Name} culture:"
printfn $" {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 定義的模式來格式化。 傳回值與使用 ToString(String) 方法指定 「d」標準 DateTime 格式字串所傳回的值相同。
注意
方法傳回的 ToShortDateString 字串會區分文化特性。 它會反映目前文化特性的 DateTimeFormatInfo.ShortDatePattern 屬性所定義的模式。 例如,針對 en-US 文化特性,標準簡短日期模式為 「M/d/yyyy」;針對 de-DE 文化特性,其為 「dd.MM.yyyy」;針對 ja-JP 文化特性,其為 「yyyy/MM/dd」。 請注意,其值可能會因 .NET 實作及其版本、作業系統及其版本和使用者自訂而有所不同。
如需有關目前執行緒文化特性的詳細資訊,請參閱 CultureInfo.CurrentCulture 與 Thread.CurrentCulture 屬性。 如需有關格式字元以及其產生之輸出的詳細資訊,請參閱標準日期和時間格式字串與自訂日期和時間格式字串。 如需有關變更與格式字元關聯之格式樣式的詳細資訊,請參閱 DateTimeFormatInfo 類別。