DateTime.ToLongDateString 方法

定義

將目前 DateTime 物件的值轉換為其相等的完整日期 (Long Date) 字串表示。

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

傳回

字串,內含目前 DateTime 物件的完整日期字串表示。

範例

下列範例示範 ToLongDateString 方法。

open System
open System.Globalization

// Initialize a DateTime object.
printfn "Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n"
let dateAndTime = DateTime(2001, 5, 16, 3, 2, 15)

// Display the name of the current culture.
printfn $"Current culture: \"{CultureInfo.CurrentCulture.Name}\"\n"
let dtfi = CultureInfo.CurrentCulture.DateTimeFormat

// Display the long date pattern and string.
printfn $"Long date pattern: \"{dtfi.LongDatePattern}\""
printfn $"Long date string:  \"{dateAndTime.ToLongDateString()}\"\n"

// Display the long time pattern and string.
printfn $"Long time pattern: \"{dtfi.LongTimePattern}\""
printfn $"Long time string:  \"{dateAndTime.ToLongTimeString()}\"\n"

// Display the short date pattern and string.
printfn $"Short date pattern: \"{dtfi.ShortDatePattern}\""
printfn $"Short date string:  \"{dateAndTime.ToShortDateString()}\"\n"

// Display the short time pattern and string.
printfn $"Short time pattern: \"{dtfi.ShortTimePattern}\""
printfn $"Short time string:  \"{dateAndTime.ToShortTimeString()}\"\n"

// The example displays output similar to the following:
//        Current culture: "en-US"
//
//        Long date pattern: "dddd, MMMM d, yyyy"
//        Long date string:  "Wednesday, May 16, 2001"
//
//        Long time pattern: "h:mm:ss tt"
//        Long time string:  "3:02:15 AM"
//
//        Short date pattern: "M/d/yyyy"
//        Short date string:  "5/16/2001"
//
//        Short time pattern: "h:mm tt"
//        Short time string:  "3:02 AM"
using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
        // Initialize a DateTime object.
        Console.WriteLine("Initialize the DateTime object to May 16, 2001 3:02:15 AM.\n");
        DateTime dateAndTime = new System.DateTime(2001, 5, 16, 3, 2, 15);

        // Display the name of the current culture.
        Console.WriteLine($"Current culture: \"{CultureInfo.CurrentCulture.Name}\"\n");
        var dtfi = CultureInfo.CurrentCulture.DateTimeFormat;

        // Display the long date pattern and string.
        Console.WriteLine($"Long date pattern: \"{dtfi.LongDatePattern}\"");
        Console.WriteLine($"Long date string:  \"{dateAndTime.ToLongDateString()}\"\n");

        // Display the long time pattern and string.
        Console.WriteLine($"Long time pattern: \"{dtfi.LongTimePattern}\"");
        Console.WriteLine($"Long time string:  \"{dateAndTime.ToLongTimeString()}\"\n");

        // Display the short date pattern and string.
        Console.WriteLine($"Short date pattern: \"{dtfi.ShortDatePattern}\"");
        Console.WriteLine($"Short date string:  \"{dateAndTime.ToShortDateString()}\"\n");

        // Display the short time pattern and string.
        Console.WriteLine($"Short time pattern: \"{dtfi.ShortTimePattern}\"");
        Console.WriteLine($"Short time string:  \"{dateAndTime.ToShortTimeString()}\"\n");
    }
}
// The example displays output similar to the following:
//        Current culture: "en-US"
//
//        Long date pattern: "dddd, MMMM d, yyyy"
//        Long date string:  "Wednesday, May 16, 2001"
//
//        Long time pattern: "h:mm:ss tt"
//        Long time string:  "3:02:15 AM"
//
//        Short date pattern: "M/d/yyyy"
//        Short date string:  "5/16/2001"
//
//        Short time pattern: "h:mm tt"
//        Short time string:  "3:02 AM"
Imports System.Globalization

Module Example
    Public Sub Main() 
        ' Initialize a DateTime object.
        Console.WriteLine($"Initialize the DateTime object to May 16, 2001 3:02:15 AM.{vbCrLf}")
        Dim dateAndTime As New DateTime(2001, 5, 16, 3, 2, 15)
        
        ' Display the name of the current culture.
        Console.WriteLine($"Current culture: ""{CultureInfo.CurrentCulture.Name}""{vbCrLf}")
        Dim dtfi = CultureInfo.CurrentCulture.DateTimeFormat
        
        ' Display the long date pattern and string.
        Console.WriteLine($"Long date pattern: ""{dtfi.LongDatePattern}""")
        Console.WriteLine($"Long date string:  ""{dateAndTime.ToLongDateString()}{vbCrLf}")
        
        ' Display the long time pattern and string.
        Console.WriteLine($"Long time pattern: ""{0}""", dtfi.LongTimePattern)
        Console.WriteLine($"Long time string:  ""{dateAndTime.ToLongTimeString()}{vbCrLf}")
        
        ' Display the short date pattern and string.
        Console.WriteLine($"Short date pattern: ""{dtfi.ShortDatePattern}""")
        Console.WriteLine($"Short date string:  ""{dateAndTime.ToShortDateString()}{vbCrLf}")
        
        ' Display the short time pattern and string.
        Console.WriteLine($"Short time pattern: ""{dtfi.ShortTimePattern}""")
        Console.WriteLine($"Short time string:  ""{dateAndTime.ToShortTimeString()}{vbCrLf}")
    End Sub
End Module
' The example displays output like the following:
'       Initialize the DateTime object to May 16, 2001 3:02:15 AM.
'
'       Current culture: "en-US"
'
'       Long date pattern: "dddd, MMMM d, yyyy"
'       Long date string:  "Wednesday, May 16, 2001
'
'       Long time pattern: "0"
'       Long time string:  "3:02:15 AM
'
'       Short date pattern: "M/d/yyyy"
'       Short date string:  "5/16/2001
'
'       Short time pattern: "h:mm tt"
'       Short time string:  "3:02 AM

備註

目前物件的值會使用與目前 DateTime 文化特性相關聯的 屬性所 LongDatePattern 定義的模式來格式化。 傳回值與使用 方法指定 「D」 標準 DateTime 格式字串ToString(String) 所傳回的值相同。

注意

方法傳回的 ToLongDateString 字串會區分文化特性。 它反映目前文化特性 DateTimeFormatInfo.LongDatePattern 的 屬性所定義的模式。 例如,針對 en-US 文化特性,標準長日期模式通常是 「dddd, MMMM dd, yyyy」;針對 de-DE 文化特性,其為 「ddd, d. MMMM yyyy「;針對 ja-JP 文化特性,其為 「yyyy'年'M'月'd'日'」。 請注意,其值可能會因 .NET 實作及其版本、作業系統及其版本和使用者自訂而有所不同。

如需有關目前執行緒文化特性的詳細資訊,請參閱 CultureInfo.CurrentCultureThread.CurrentCulture 屬性。 如需有關格式字元以及其產生之輸出的詳細資訊,請參閱標準日期和時間格式字串自訂日期和時間格式字串。 如需有關變更與格式字元關聯之格式樣式的詳細資訊,請參閱 DateTimeFormatInfo 類別。

適用於

另請參閱