DateTime.ToShortTimeString DateTime.ToShortTimeString DateTime.ToShortTimeString DateTime.ToShortTimeString Method

定义

将当前 DateTime 对象的值转换为其等效的短时间字符串表示形式。Converts the value of the current DateTime object to its equivalent short time string representation.

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

返回

一个字符串,它包含当前 DateTime 对象的短时间字符串表示形式。A string that contains the short time string representation of the current DateTime object.

示例

下面的示例演示ToShortTimeString方法。The following example demonstrates the ToShortTimeString method.

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 'Main
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对象的格式使用定义的模式DateTimeFormatInfo.ShortTimePattern与当前线程区域性关联的属性。The value of the current DateTime object is formatted using the pattern defined by the DateTimeFormatInfo.ShortTimePattern property associated with the current thread culture. 返回值是通过指定"t"返回的值相同标准 DateTime 格式字符串ToString(String)方法。The return value is identical to the value returned by specifying the "t" standard DateTime format string with the ToString(String) method.

备注

返回的字符串ToShortTimeString方法是区分区域性的。The string returned by the ToShortTimeString method is culture-sensitive. 它反映当前区域性定义的模式DateTimeFormatInfo.ShortTimePattern属性。It reflects the pattern defined by the current culture's DateTimeFormatInfo.ShortTimePattern property. 例如,对于 EN-US 区域性,标准短时间模式为"h:mm tt";对于 DE-DE 区域性中,它是"hh: mm";对于 JA-JP 区域性,它是"H:mm"。For example, for the en-US culture, the standard short time pattern is "h:mm tt"; for the de-DE culture, it is "HH:mm"; for the ja-JP culture, it is "H:mm". 请注意,其值可以有所不同的.NET 实现和它的版本、 操作系统和其版本和用户自定义。Note that its value can vary depending on the .NET implementation and its version, the operating system and its version, and user customization.

有关当前线程区域性的详细信息,请参阅 CultureInfo.CurrentCultureThread.CurrentCulture 属性。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. 有关更改与格式字符相关的格式模式的详细信息,请参阅 DateTimeFormatInfo 类。For more information about changing the format pattern associated with a format character, see the DateTimeFormatInfo class.

适用于

另请参阅