英語で読む

次の方法で共有


DateTime.ToShortTimeString メソッド

定義

現在の DateTime オブジェクトの値を、それと等価な短い形式の時刻の文字列形式に変換します。

C#
public string ToShortTimeString ();

戻り値

String

現在の DateTime オブジェクトを短い形式の時刻で表した文字列。

ToShortTimeStringメソッドの例を次に示します。

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"

注釈

現在のオブジェクトの値は、現在 DateTime のカルチャに関連付けられているプロパティによって定義されたパターンを DateTimeFormatInfo.ShortTimePattern 使用して書式設定されます。 戻り値は、メソッドで "t" 標準の DateTime 書式指定文字列 を指定して返される値と ToString(String) 同じです。

注意

メソッドによって ToShortTimeString 返される文字列はカルチャに依存します。 現在のカルチャのプロパティによって定義されたパターンが DateTimeFormatInfo.ShortTimePattern 反映されます。 たとえば、en-US カルチャの場合、標準の短い時間パターンは "h:mm tt" です。de-DE カルチャの場合は "HH:mm" です。ja-JP カルチャの場合、"H:mm" です。 その値は、.NET の実装とそのバージョン、オペレーティング システムとそのバージョン、およびユーザーのカスタマイズによって異なる場合があることに注意してください。

現在のスレッド カルチャについて詳しくは、CultureInfo.CurrentCulture および Thread.CurrentCulture プロパティを参照してください。 書式指定文字、書式パターン、およびそれらによって生成される出力について詳しくは、「標準の日時書式指定文字列」と「カスタム日時書式指定文字列」をご覧ください。 書式指定文字に関連付けられている書式パターンの変更について詳しくは、DateTimeFormatInfo クラスをご覧ください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.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
.NET Standard 2.0, 2.1

こちらもご覧ください