DateTime.ToShortTimeString 方法

定義

將目前 DateTime 物件的值轉換為其相等的簡短時間 (Short Time) 字串表示。

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

傳回

String

字串,內含目前 DateTime 物件的簡短時間字串表示。

範例

下列範例會示範 ToShortTimeString 方法。

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 會使用 DateTimeFormatInfo.ShortTimePattern 與目前文化特性相關聯之屬性所定義的模式來格式化。 傳回值與使用方法指定 "t" 標準日期時間格式字串 所傳回的值相同 ToString(String)

注意

方法所傳回的字串 ToShortTimeString 會區分文化特性。 它會反映目前文化特性的屬性所定義的模式 DateTimeFormatInfo.ShortTimePattern 。 例如,針對 en-us 文化特性,標準短時間模式為 "h:mm tt";若為 de 刪除文化特性,則為 "HH: mm";若是 ja-jp 文化特性,則為 "H:mm"。 請注意,其值可能會隨著 .NET 的執行及其版本、作業系統及其版本以及使用者自訂而有所不同。

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

適用於

另請參閱