英語で読む

次の方法で共有


DateTime.ToLongTimeString メソッド

定義

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

C#
public string ToLongTimeString ();

戻り値

String

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

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

C#
using System;
using System.Threading;
using System.Globalization;

public class Sample
{
   public static void Main()
   {
      // Create an array of culture names.
      String[] names = { "en-US", "en-GB", "fr-FR", "de-DE" };
      // Initialize a DateTime object.
      DateTime dateValue = new System.DateTime(2013, 5, 28, 10, 30, 15);

      // Iterate the array of culture names.
      foreach (var name in names) {
         // Change the culture of the current thread.
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(name);
         // Display the name of the current culture and the date.
         Console.WriteLine("Current culture: {0}", CultureInfo.CurrentCulture.Name);
         Console.WriteLine("Date: {0:G}", dateValue);

         // Display the long time pattern and the long time.
         Console.WriteLine("Long time pattern: '{0}'",
                           DateTimeFormatInfo.CurrentInfo.LongTimePattern);
         Console.WriteLine("Long time with format string:     {0:T}", dateValue);
         Console.WriteLine("Long time with ToLongTimeString:  {0}\n",
                           dateValue.ToLongTimeString());
      }
   }
}
// The example displays the following output:
//       Current culture: en-US
//       Date: 5/28/2013 10:30:15 AM
//       Long time pattern: 'h:mm:ss tt'
//       Long time with format string:     10:30:15 AM
//       Long time with ToLongTimeString:  10:30:15 AM
//
//       Current culture: en-GB
//       Date: 28/05/2013 10:30:15
//       Long time pattern: 'HH:mm:ss'
//       Long time with format string:     10:30:15
//       Long time with ToLongTimeString:  10:30:15
//
//       Current culture: fr-FR
//       Date: 28/05/2013 10:30:15
//       Long time pattern: 'HH:mm:ss'
//       Long time with format string:     10:30:15
//       Long time with ToLongTimeString:  10:30:15
//
//       Current culture: de-DE
//       Date: 28.05.2013 10:30:15
//       Long time pattern: 'HH:mm:ss'
//       Long time with format string:     10:30:15
//       Long time with ToLongTimeString:  10:30:15

注釈

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

注意

メソッドによって ToLongTimeString 返される文字列はカルチャに依存します。 現在のカルチャのプロパティによって定義されたパターンが DateTimeFormatInfo.LongTimePattern 反映されます。 たとえば、en-US カルチャの場合、標準の長い時間パターンは "h:mm:ss tt" です。de-DE カルチャの場合、"HH:mm:ss" です。ja-JP カルチャの場合、"H:mm:ss" です。 その値は、.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

こちらもご覧ください