英語で読む

次の方法で共有


DateTime.ToString メソッド

定義

現在の DateTime オブジェクトの値を等価の文字列形式に変換します。

オーバーロード

ToString(IFormatProvider)

指定したカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

ToString(String)

指定した形式および現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

ToString()

現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

ToString(String, IFormatProvider)

指定した書式およびカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価の文字列形式に変換します。

ToString(IFormatProvider)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

指定したカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

C#
public string ToString (IFormatProvider provider);
C#
public string ToString (IFormatProvider? provider);

パラメーター

provider
IFormatProvider

カルチャ固有の書式情報を提供するオブジェクト。

戻り値

DateTime により指定された、現在の provider オブジェクトの値の文字列形式。

実装

例外

日時が、provider で使用されるカレンダーでサポートされている日付の範囲外です。

次の例では、5 つの異なるカルチャを表すオブジェクトを使用して CultureInfo 、日付と時刻の文字列表現を表示します。

C#
using System;
using System.Globalization;

public class ToStringExample3
{
   public static void Main2()
   {
      CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture,
                                                  new CultureInfo("en-us"),
                                                  new CultureInfo("fr-fr"),
                                                  new CultureInfo("de-DE"),
                                                  new CultureInfo("es-ES"),
                                                  new CultureInfo("ja-JP")};

      DateTime thisDate = new DateTime(2009, 5, 1, 9, 0, 0);

      foreach (CultureInfo culture in cultures)
      {
         string cultureName;
         if (string.IsNullOrEmpty(culture.Name))
            cultureName = culture.NativeName;
         else
            cultureName = culture.Name;

         Console.WriteLine("In {0}, {1}",
                           cultureName, thisDate.ToString(culture));
      }
      }
}
// The example produces the following output:
//    In Invariant Language (Invariant Country), 05/01/2009 09:00:00
//    In en-US, 5/1/2009 9:00:00 AM
//    In fr-FR, 01/05/2009 09:00:00
//    In de-DE, 01.05.2009 09:00:00
//    In es-ES, 01/05/2009 9:00:00
//    In ja-JP, 2009/05/01 9:00:00

注釈

現在 DateTime のオブジェクトの値は、短い日付パターンと長い時間パターンを使用して出力を書式設定する一般的な日付と時刻の書式指定子 ('G') を使用して書式設定されます。

短い日付と長い時刻のパターンの形式は、 provider パラメーターによって定義されます。 providerパラメーターには、次のいずれかを指定できます。

  • CultureInfo書式設定規則が返される文字列に反映されるカルチャを表す オブジェクト。 プロパティによってCultureInfo.DateTimeFormat返される オブジェクトはDateTimeFormatInfo、返される文字列の書式設定を定義します。

  • DateTimeFormatInfo日付と時刻のデータの形式を定義する オブジェクト。

  • インターフェイスを実装する IFormatProvider カスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を DateTimeFormatInfo 提供する オブジェクトを返します。

nullの場合providerは、現在のDateTimeFormatInfoカルチャに関連付けられているオブジェクトが使用されます。 詳細については、「CultureInfo.CurrentCulture」を参照してください。

注意 (呼び出し元)

メソッドは ToString(IFormatProvider) 、 パラメーターで表されるカルチャによって使用されるカレンダー内の日付と時刻の文字列表現を provider 返します。 そのカレンダーは、 プロパティによって Calendar 定義されます。 現在 DateTime のインスタンスの値が より MinSupportedDateTime 前またはそれより後 MaxSupportedDateTimeの場合、メソッドは を ArgumentOutOfRangeExceptionスローします。 具体的な例を次に示します。 クラスの範囲外の日付の書式設定を JapaneseCalendar 試みます。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo jaJP = new CultureInfo("ja-JP");
      jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
      DateTime date1 = new DateTime(1867, 1, 1);

      try {
         Console.WriteLine(date1.ToString(jaJP));
      }
      catch (ArgumentOutOfRangeException) {
         Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
                           date1,
                           jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
                           jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
      }
   }
}
// The example displays the following output:
//    1/1/1867 is earlier than 9/8/1868 or later than 12/31/9999   }

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

指定した形式および現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

C#
public string ToString (string format);
C#
public string ToString (string? format);

パラメーター

format
String

標準またはカスタムの日時書式指定文字列。

戻り値

DateTime により指定された、現在の format オブジェクトの値の文字列形式。

例外

format は長さが 1 であり、DateTimeFormatInfo で定義されている書式指定文字の 1 つではありません。

または

format には有効なカスタム書式パターンがありません。

日時が、現在のカルチャで使用されるカレンダーでサポートされている日付の範囲外です。

次の例では、標準の日付と時刻の書式指定文字列と、カスタムの日付と時刻の書式指定文字列の選択を使用して、値の文字列表現を DateTime 表示します。 この例のスレッドの現在のカルチャは en-US です。

C#
using System;

public class DateToStringExample2
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2008, 6, 15, 21, 15, 07);
      // Create an array of standard format strings.
      string[] standardFmts = {"d", "D", "f", "F", "g", "G", "m", "o",
                               "R", "s", "t", "T", "u", "U", "y"};
      // Output date and time using each standard format string.
      foreach (string standardFmt in standardFmts)
         Console.WriteLine("{0}: {1}", standardFmt,
                           dateValue.ToString(standardFmt));
      Console.WriteLine();

      // Create an array of some custom format strings.
      string[] customFmts = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f",
                             "dd MMM HH:mm:ss", @"\Mon\t\h\: M", "HH:mm:ss.ffffzzz" };
      // Output date and time using each custom format string.
      foreach (string customFmt in customFmts)
         Console.WriteLine("'{0}': {1}", customFmt,
                           dateValue.ToString(customFmt));
   }
}
// This example displays the following output to the console:
//       d: 6/15/2008
//       D: Sunday, June 15, 2008
//       f: Sunday, June 15, 2008 9:15 PM
//       F: Sunday, June 15, 2008 9:15:07 PM
//       g: 6/15/2008 9:15 PM
//       G: 6/15/2008 9:15:07 PM
//       m: June 15
//       o: 2008-06-15T21:15:07.0000000
//       R: Sun, 15 Jun 2008 21:15:07 GMT
//       s: 2008-06-15T21:15:07
//       t: 9:15 PM
//       T: 9:15:07 PM
//       u: 2008-06-15 21:15:07Z
//       U: Monday, June 16, 2008 4:15:07 AM
//       y: June, 2008
//
//       'h:mm:ss.ff t': 9:15:07.00 P
//       'd MMM yyyy': 15 Jun 2008
//       'HH:mm:ss.f': 21:15:07.0
//       'dd MMM HH:mm:ss': 15 Jun 21:15:07
//       '\Mon\t\h\: M': Month: 6
//       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00

注釈

メソッドは ToString(String) 、現在のカルチャの書式設定規則を使用する特定の形式の日付と時刻の値の文字列形式を返します。詳細については、「」を参照してください CultureInfo.CurrentCulture

パラメーターには format 、1 つの書式指定子文字 ( 「標準の日付と時刻の書式指定文字列」を参照) または、返される文字列の形式を定義するカスタム書式指定パターン ( 「カスタム日時書式指定文字列」を参照) が含まれている必要があります。 が null または空の文字列の場合formatは、一般的な書式指定子 'G' が使用されます。

このメソッドの一部の用途は次のとおりです。

  • 現在のカルチャの短い日付と時刻の形式で日付と時刻を表示する文字列を取得します。 これを行うには、"G" 書式指定子を使用します。

  • 月と年のみを含む文字列を取得します。 これを行うには、"MM/yyyy" 書式指定文字列を使用します。 書式指定文字列は、現在のカルチャの日付区切り記号を使用します。

  • 特定の形式の日付と時刻を含む文字列を取得します。 たとえば、"MM/dd/yyyyHHH:mm" 書式指定文字列は、"19//03//2013 18:06" などの固定形式で日付と時刻の文字列を表示します。 書式指定文字列では、カルチャ固有の設定に関係なく、固定日付区切り記号として "/" が使用されます。

  • 日付文字列をシリアル化するために使用できる要約形式で日付を取得します。 たとえば、"yyyyMMdd" 書式指定文字列には、4 桁の年の後に 2 桁の月と日付区切り記号のない 2 桁の日が表示されます。

次の例では、これら 3 つの書式指定文字列を使用して、en-US カルチャと fr-FR カルチャの規則を使用して日付と時刻の値を表示します。

C#
using System;
using System.Globalization;

public class ToStringExample5
{
    public static void Main3()
    {
        string[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm", "yyyyMMdd" };
        string[] cultureNames = { "en-US", "fr-FR" };
        DateTime date = new DateTime(2015, 8, 18, 13, 31, 17);
        foreach (var cultureName in cultureNames)
        {
            var culture = new CultureInfo(cultureName);
            CultureInfo.CurrentCulture = culture;
            Console.WriteLine(culture.NativeName);
            foreach (var format in formats)
                Console.WriteLine($"   {format}: {date.ToString(format)}");
            Console.WriteLine();
        }
    }
}
// The example displays the following output:
//       English (United States)
//          G: 8/18/2015 1:31:17 PM
//          MM/yyyy: 08/2015
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31
//          yyyyMMdd: 20150818
//
//       français (France)
//          G: 18/08/2015 13:31:17
//          MM/yyyy: 08/2015
//          MM\/dd\/yyyy HH:mm: 08/18/2015 13:31
//          yyyyMMdd: 20150818

注意 (呼び出し元)

メソッドは ToString(String) 、現在のカルチャで使用されるカレンダーの日付と時刻の文字列表現を返します。 現在 DateTime のインスタンスの値が より MinSupportedDateTime 前またはそれより後 MaxSupportedDateTimeの場合、メソッドは を ArgumentOutOfRangeExceptionスローします。 具体的な例を次に示します。 現在のカルチャがヘブライ語 (イスラエル) の場合、クラスの範囲外の HebrewCalendar 日付を書式設定しようとします。

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

public class Example3
{
    public static void Main()
    {
        DateTime date1 = new DateTime(1550, 7, 21);
        CultureInfo dft;
        CultureInfo heIL = new CultureInfo("he-IL");
        heIL.DateTimeFormat.Calendar = new HebrewCalendar();

        // Change current culture to he-IL.
        dft = Thread.CurrentThread.CurrentCulture;
        Thread.CurrentThread.CurrentCulture = heIL;

        // Display the date using the current culture's calendar.
        try
        {
            Console.WriteLine(date1.ToString("G"));
        }
        catch (ArgumentOutOfRangeException)
        {
            Console.WriteLine("{0} is earlier than {1} or later than {2}",
                              date1.ToString("d", CultureInfo.InvariantCulture),
                              heIL.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
                              heIL.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
        }

        // Restore the default culture.
        Thread.CurrentThread.CurrentCulture = dft;
    }
}
// The example displays the following output:
//    07/21/1550 is earlier than 01/01/1583 or later than 09/29/2239

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString()

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

現在のカルチャの書式指定規則を使用して、現在の DateTime オブジェクトの値をそれと等価な文字列形式に変換します。

C#
public override string ToString ();

戻り値

現在の DateTime オブジェクトの値の文字列形式。

例外

日時が、現在のカルチャで使用されるカレンダーでサポートされている日付の範囲外です。

次の例は、 メソッドによってToString()返される値の文字列表現がスレッドの現在のDateTimeカルチャにどのように依存するかを示しています。 現在のカルチャを en-US、fr-FR、ja-JP に変更し、いずれの場合も メソッドを ToString() 呼び出して、そのカルチャを使用して日付と時刻の値の文字列表現を返します。

C#
using System;
using System.Globalization;

public class ToStringExample1
{
    public static void Main()
    {
        CultureInfo currentCulture = CultureInfo.CurrentCulture;
        DateTime exampleDate = new DateTime(2021, 5, 1, 18, 32, 6);

        // Change the current culture to en-US and display the date.
        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US");
        Console.WriteLine(exampleDate.ToString());

        // Change the current culture to fr-FR and display the date.
        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
        Console.WriteLine(exampleDate.ToString());

        // Change the current culture to ja-JP and display the date.
        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("ja-JP");
        Console.WriteLine(exampleDate.ToString());

        // Restore the original culture
        CultureInfo.CurrentCulture = currentCulture;
    }
}

// The example displays the following output to the console:
//       5/1/2021 6:32:06 PM
//       01/05/2021 18:32:06
//       2021/05/01 18:32:06

注釈

現在 DateTime のオブジェクトの値は、一般的な日時書式指定子 ('G') を使用して書式設定されます。 特定の日時書式指定子を使用して書式を設定するには、 メソッドを ToString(String) 呼び出します。 特定のカルチャに対して一般的な日付と時刻の書式指定子 ('G') を使用して書式を設定するには、 メソッドを ToString(IFormatProvider) 呼び出します。 特定の日付と時刻の書式指定子と特定のカルチャの規則を使用して書式を設定するには、 メソッドを ToString(String, IFormatProvider) 呼び出します。

このメソッドは、現在のカルチャから派生した書式設定情報を使用します。 特に、 プロパティによって返されるカスタム書式指定文字列とLongTimePattern、 プロパティによってShortDatePattern返されるオブジェクトのプロパティをDateTimeFormatInfoThread.CurrentThread.CurrentCulture.DateTimeFormat組み合わせます。 詳細については、「CultureInfo.CurrentCulture」を参照してください。 メソッドの他の ToString オーバーロードを使用すると、使用する書式を持つカルチャを指定し、値の出力パターンを DateTime 定義できます。

注意 (呼び出し元)

メソッドは ToString() 、現在のカルチャで使用されるカレンダーの日付と時刻の文字列表現を返します。 現在 DateTime のインスタンスの値が より MinSupportedDateTime 前またはそれより後 MaxSupportedDateTimeの場合、メソッドは を ArgumentOutOfRangeExceptionスローします。 具体的な例を次に示します。 現在のカルチャがアラビア語 (シリア) の場合、クラスの範囲外の HijriCalendar 日付を書式設定しようとします。

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

public class Example2
{
    public static void Main()
    {
        DateTime date1 = new DateTime(550, 1, 1);
        CultureInfo dft;
        CultureInfo arSY = new CultureInfo("ar-SY");
        arSY.DateTimeFormat.Calendar = new HijriCalendar();

        // Change current culture to ar-SY.
        dft = Thread.CurrentThread.CurrentCulture;
        Thread.CurrentThread.CurrentCulture = arSY;

        // Display the date using the current culture's calendar.
        try
        {
            Console.WriteLine(date1.ToString());
        }
        catch (ArgumentOutOfRangeException)
        {
            Console.WriteLine("{0} is earlier than {1} or later than {2}",
                              date1.ToString("d", CultureInfo.InvariantCulture),
                              arSY.DateTimeFormat.Calendar.MinSupportedDateTime.ToString("d", CultureInfo.InvariantCulture),
                              arSY.DateTimeFormat.Calendar.MaxSupportedDateTime.ToString("d", CultureInfo.InvariantCulture));
        }

        // Restore the default culture.
        Thread.CurrentThread.CurrentCulture = dft;
    }
}
// The example displays the following output:
//    01/01/0550 is earlier than 07/18/0622 or later than 12/31/9999

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToString(String, IFormatProvider)

ソース:
DateTime.cs
ソース:
DateTime.cs
ソース:
DateTime.cs

指定した書式およびカルチャ固有の書式情報を使用して、現在の DateTime オブジェクトの値をそれと等価の文字列形式に変換します。

C#
public string ToString (string format, IFormatProvider provider);
C#
public string ToString (string? format, IFormatProvider? provider);

パラメーター

format
String

標準またはカスタムの日時書式指定文字列。

provider
IFormatProvider

カルチャ固有の書式情報を提供するオブジェクト。

戻り値

DateTime および format により指定された、現在の provider オブジェクトの値の文字列形式。

実装

例外

format は長さが 1 であり、DateTimeFormatInfo で定義されている書式指定文字の 1 つではありません。

または

format には有効なカスタム書式パターンがありません。

日時が、provider で使用されるカレンダーでサポートされている日付の範囲外です。

次の例では、標準の日付時刻書式指定文字列を使用して、4 つの異なるカルチャの日付と時刻の文字列表現を表示します。

C#
using System;
using System.Globalization;

public class ToStringExample4
{
   public static void Main1()
   {
      // Create an array of all supported standard date and time format specifiers.
      string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r",
                          "s", "t", "T", "u", "U", "Y"};
      // Create an array of four cultures.
      CultureInfo[] cultures = {CultureInfo.GetCultureInfo("de-DE"),
                                CultureInfo.GetCultureInfo("en-US"),
                                CultureInfo.GetCultureInfo("es-ES"),
                                CultureInfo.GetCultureInfo("fr-FR")};
       // Define date to be displayed.
      DateTime dateToDisplay = new DateTime(2008, 10, 31, 17, 4, 32);

      // Iterate each standard format specifier.
      foreach (string formatSpecifier in formats)
      {
         foreach (CultureInfo culture in cultures)
            Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}",
                              formatSpecifier, culture.Name,
                              dateToDisplay.ToString(formatSpecifier, culture));
         Console.WriteLine();
      }
   }
}

// The example displays the following output:
//    d Format Specifier      de-DE Culture                               31.10.2008
//    d Format Specifier      en-US Culture                               10/31/2008
//    d Format Specifier      es-ES Culture                               31/10/2008
//    d Format Specifier      fr-FR Culture                               31/10/2008
//    
//    D Format Specifier      de-DE Culture                Freitag, 31. Oktober 2008
//    D Format Specifier      en-US Culture                 Friday, October 31, 2008
//    D Format Specifier      es-ES Culture           viernes, 31 de octubre de 2008
//    D Format Specifier      fr-FR Culture                 vendredi 31 octobre 2008
//    
//    f Format Specifier      de-DE Culture          Freitag, 31. Oktober 2008 17:04
//    f Format Specifier      en-US Culture         Friday, October 31, 2008 5:04 PM
//    f Format Specifier      es-ES Culture     viernes, 31 de octubre de 2008 17:04
//    f Format Specifier      fr-FR Culture           vendredi 31 octobre 2008 17:04
//    
//    F Format Specifier      de-DE Culture       Freitag, 31. Oktober 2008 17:04:32
//    F Format Specifier      en-US Culture      Friday, October 31, 2008 5:04:32 PM
//    F Format Specifier      es-ES Culture  viernes, 31 de octubre de 2008 17:04:32
//    F Format Specifier      fr-FR Culture        vendredi 31 octobre 2008 17:04:32
//    
//    g Format Specifier      de-DE Culture                         31.10.2008 17:04
//    g Format Specifier      en-US Culture                       10/31/2008 5:04 PM
//    g Format Specifier      es-ES Culture                         31/10/2008 17:04
//    g Format Specifier      fr-FR Culture                         31/10/2008 17:04
//    
//    G Format Specifier      de-DE Culture                      31.10.2008 17:04:32
//    G Format Specifier      en-US Culture                    10/31/2008 5:04:32 PM
//    G Format Specifier      es-ES Culture                      31/10/2008 17:04:32
//    G Format Specifier      fr-FR Culture                      31/10/2008 17:04:32
//    
//    m Format Specifier      de-DE Culture                              31. Oktober
//    m Format Specifier      en-US Culture                               October 31
//    m Format Specifier      es-ES Culture                            31 de octubre
//    m Format Specifier      fr-FR Culture                               31 octobre
//    
//    o Format Specifier      de-DE Culture              2008-10-31T17:04:32.0000000
//    o Format Specifier      en-US Culture              2008-10-31T17:04:32.0000000
//    o Format Specifier      es-ES Culture              2008-10-31T17:04:32.0000000
//    o Format Specifier      fr-FR Culture              2008-10-31T17:04:32.0000000
//    
//    r Format Specifier      de-DE Culture            Fri, 31 Oct 2008 17:04:32 GMT
//    r Format Specifier      en-US Culture            Fri, 31 Oct 2008 17:04:32 GMT
//    r Format Specifier      es-ES Culture            Fri, 31 Oct 2008 17:04:32 GMT
//    r Format Specifier      fr-FR Culture            Fri, 31 Oct 2008 17:04:32 GMT
//    
//    s Format Specifier      de-DE Culture                      2008-10-31T17:04:32
//    s Format Specifier      en-US Culture                      2008-10-31T17:04:32
//    s Format Specifier      es-ES Culture                      2008-10-31T17:04:32
//    s Format Specifier      fr-FR Culture                      2008-10-31T17:04:32
//    
//    t Format Specifier      de-DE Culture                                    17:04
//    t Format Specifier      en-US Culture                                  5:04 PM
//    t Format Specifier      es-ES Culture                                    17:04
//    t Format Specifier      fr-FR Culture                                    17:04
//    
//    T Format Specifier      de-DE Culture                                 17:04:32
//    T Format Specifier      en-US Culture                               5:04:32 PM
//    T Format Specifier      es-ES Culture                                 17:04:32
//    T Format Specifier      fr-FR Culture                                 17:04:32
//    
//    u Format Specifier      de-DE Culture                     2008-10-31 17:04:32Z
//    u Format Specifier      en-US Culture                     2008-10-31 17:04:32Z
//    u Format Specifier      es-ES Culture                     2008-10-31 17:04:32Z
//    u Format Specifier      fr-FR Culture                     2008-10-31 17:04:32Z
//    
//    U Format Specifier      de-DE Culture       Freitag, 31. Oktober 2008 09:04:32
//    U Format Specifier      en-US Culture      Friday, October 31, 2008 9:04:32 AM
//    U Format Specifier      es-ES Culture   viernes, 31 de octubre de 2008 9:04:32
//    U Format Specifier      fr-FR Culture        vendredi 31 octobre 2008 09:04:32
//    
//    Y Format Specifier      de-DE Culture                             Oktober 2008
//    Y Format Specifier      en-US Culture                             October 2008
//    Y Format Specifier      es-ES Culture                          octubre de 2008
//    Y Format Specifier      fr-FR Culture                             octobre 2008

次の例では、インバリアント DateTimeFormatInfoを使用して値をDateTime書式設定するさまざまな方法を示します。

C#
using System;
using System.Globalization;

public class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt = DateTime.Now;
        String[] format = {
            "d", "D",
            "f", "F",
            "g", "G",
            "m",
            "r",
            "s",
            "t", "T",
            "u", "U",
            "y",
            "dddd, MMMM dd yyyy",
            "ddd, MMM d \"'\"yy",
            "dddd, MMMM dd",
            "M/yy",
            "dd-MM-yy",
        };
        string date;
        for (int i = 0; i < format.Length; i++)
        {
            date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
            Console.WriteLine(string.Concat(format[i], " :", date));
        }

        /** Output.
         *
         * d :08/17/2000
         * D :Thursday, August 17, 2000
         * f :Thursday, August 17, 2000 16:32
         * F :Thursday, August 17, 2000 16:32:32
         * g :08/17/2000 16:32
         * G :08/17/2000 16:32:32
         * m :August 17
         * r :Thu, 17 Aug 2000 23:32:32 GMT
         * s :2000-08-17T16:32:32
         * t :16:32
         * T :16:32:32
         * u :2000-08-17 23:32:32Z
         * U :Thursday, August 17, 2000 23:32:32
         * y :August, 2000
         * dddd, MMMM dd yyyy :Thursday, August 17 2000
         * ddd, MMM d "'"yy :Thu, Aug 17 '00
         * dddd, MMMM dd :Thursday, August 17
         * M/yy :8/00
         * dd-MM-yy :17-08-00
         */
    }
}

注釈

パラメーターには format 、1 つの書式指定子文字 ( 「標準の日付と時刻の書式指定文字列」を参照) またはカスタム書式指定パターンを含めることができます (「 カスタム日付と時刻の書式指定文字列」を参照)。 が null または空の文字列 ("") の場合formatは、標準書式指定子 "G" が使用されます。

パラメーターは provider 、標準書式指定子に対応するパターンと、日付と時刻のコンポーネントのシンボルと名前を定義します。 providerパラメーターには、次のいずれかを指定できます。

  • CultureInfo書式設定規則が返される文字列に反映されるカルチャを表す オブジェクト。 プロパティによってCultureInfo.DateTimeFormat返される オブジェクトはDateTimeFormatInfo、返される文字列の書式設定を定義します。

  • DateTimeFormatInfo日付と時刻のデータの形式を定義する オブジェクト。

  • インターフェイスを実装する IFormatProvider カスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を DateTimeFormatInfo 提供する オブジェクトを返します。

nullの場合providerは、現在のDateTimeFormatInfoカルチャに関連付けられている が使用されます。 詳細については、「CultureInfo.CurrentCulture」を参照してください。

注意 (呼び出し元)

メソッドは ToString(String, IFormatProvider) 、 パラメーターで使用されるカレンダーの日付と時刻の文字列表現を provider 返します。 そのカレンダーは、 プロパティによって Calendar 定義されます。 現在 DateTime のインスタンスの値が より MinSupportedDateTime 前またはそれより後 MaxSupportedDateTimeの場合、メソッドは を ArgumentOutOfRangeExceptionスローします。 具体的な例を次に示します。 クラスの範囲外の日付の書式設定を UmAlQuraCalendar 試みます。

C#
using System;
using System.Globalization;

public class Example4
{
    public static void Main()
    {
        CultureInfo arSA = new CultureInfo("ar-SA");
        arSA.DateTimeFormat.Calendar = new UmAlQuraCalendar();
        DateTime date1 = new DateTime(1890, 9, 10);

        try
        {
            Console.WriteLine(date1.ToString("d", arSA));
        }
        catch (ArgumentOutOfRangeException)
        {
            Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
                              date1,
                              arSA.DateTimeFormat.Calendar.MinSupportedDateTime,
                              arSA.DateTimeFormat.Calendar.MaxSupportedDateTime);
        }
    }
}
// The example displays the following output:
//    9/10/1890 is earlier than 4/30/1900 or later than 5/13/2029

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, 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 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0