DateTime.ToLongDateString Metoda

Definicja

Konwertuje wartość bieżącego DateTime obiektu na równoważną reprezentację ciągu daty długiej.

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

Zwraca

String

Ciąg zawierający ciąg daty długiej reprezentujący bieżący DateTime obiekt.

Przykłady

W poniższym przykładzie pokazano metodę 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

Uwagi

Wartość bieżącego DateTime obiektu jest formatowana przy użyciu wzorca zdefiniowanego LongDatePattern przez właściwość skojarzona z bieżącą kulturą. Wartość zwracana jest identyczna z wartością zwracaną przez określenie standardowego ciągu formatu DateTime "D" za pomocą ToString(String) metody .

Uwaga

Ciąg zwracany przez metodę ToLongDateString jest uwzględniany w kulturze. Odzwierciedla wzorzec zdefiniowany przez właściwość bieżącej DateTimeFormatInfo.LongDatePattern kultury. Na przykład w przypadku kultury en-US standardowy wzorzec daty długiej jest często "dddd, MMMM dd, yyyy"; dla kultury de-DE jest to "dddd, d. MMMM yyyy"; dla kultury ja-JP, jest to "yyyy'年'M'M'月'd'日'". Należy pamiętać, że jego wartość może się różnić w zależności od implementacji platformy .NET i jej wersji, systemu operacyjnego i jego wersji oraz dostosowywania użytkownika.

Aby uzyskać więcej informacji na temat bieżącej kultury wątku, zobacz CultureInfo.CurrentCulture właściwości Thread.CurrentCulture i . Aby uzyskać więcej informacji na temat znaków formatu, wzorców formatu i danych wyjściowych, które są przez nie tworzyć, zobacz Ciągi standardowego formatu daty i godziny oraz Niestandardowe ciągi formatu daty i godziny. Aby uzyskać więcej informacji na temat zmiany wzorca formatu skojarzonego ze znakiem formatu, zobacz DateTimeFormatInfo klasę .

Dotyczy

Zobacz też