DateTime.ToLongDateString Method

Definition

Converts the value of the current DateTime object to its equivalent long date string representation.

C#
public string ToLongDateString ();

Returns

A string that contains the long date string representation of the current DateTime object.

Examples

The following example demonstrates the ToLongDateString method.

C#
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"

Remarks

The value of the current DateTime object is formatted using the pattern defined by the LongDatePattern property associated with the current culture. The return value is identical to the value returned by specifying the "D" standard DateTime format string with the ToString(String) method.

备注

The string returned by the ToLongDateString method is culture-sensitive. It reflects the pattern defined by the current culture's DateTimeFormatInfo.LongDatePattern property. For example, for the en-US culture, the standard long date pattern is commonly "dddd, MMMM dd, yyyy"; for the de-DE culture, it is "dddd, d. MMMM yyyy"; for the ja-JP culture, it is "yyyy'年'M'月'd'日'". Note that its value can vary depending on the .NET implementation and its version, the operating system and its version, and user customization.

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. For more information about changing the format pattern associated with a format character, see the DateTimeFormatInfo class.

Applies to

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also