閱讀英文

共用方式為


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 使用之曆法所支援的日期範圍。

範例

下列範例會使用 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 可以是下列任一項:

如果 providernull,則會 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 定義的格式規範字元。

-或-

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 應包含單一格式規範字元 (請參閱 標準日期和時間格式字串) 或自定義格式模式, (請參閱自定義 日期和時間格式字元串 ,) 定義傳回字元串的格式。 如果 formatnull 或空字串,則會使用一般格式規範 『G』。

此方法的一些用法包括:

  • 取得字串,此字串會顯示目前文化特性的簡短日期和時間格式。 若要這樣做,請使用 「G」 格式規範。

  • 取得只包含月份和年份的字串。 若要這樣做,您可以使用 「MM/yyyy」 格式字串。 格式字串會使用目前文化特性的日期分隔符。

  • 取得字串,其中包含特定格式的日期和時間。 例如,“MM/dd/yyyyH:mm” 格式字串會以固定格式顯示日期和時間字串,例如 “19/03/2013 18:06”。 不論文化特性特定設定為何,格式字串都會使用 “/” 做為固定日期分隔符。

  • 以可用來串行化日期字串的壓縮格式取得日期。 例如,「yyyyMMMdd」 格式字串會顯示四位數年份,後面接著兩位數月份,以及沒有日期分隔符的兩位數日期。

下列範例會使用這三種格式字串,藉由使用 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 屬性所ShortDatePatternThread.CurrentThread.CurrentCulture.DateTimeFormat傳回的DateTimeFormatInfo自定義格式字串。 如需詳細資訊,請參閱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 物件值的字串表示,如 formatprovider所指定。

實作

例外狀況

format 長度為 1,而且不屬於針對 DateTimeFormatInfo 定義的格式規範字元。

-或-

format 不包含有效的自訂格式模式。

日期和時間超出 provider 使用之曆法所支援的日期範圍。

範例

下列範例會使用每個標準日期時間格式字串來顯示四個不同文化特性的日期和時間字串表示。

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 可以包含單一格式規範字元 (請參閱 標準日期和時間格式字串) 或自定義格式模式, (請參閱 自定義日期和時間格式字元串) 。 如果 formatnull 或空字串 (“”“) ,則會使用標準格式規範 ”G“。

參數 provider 會定義對應至標準格式規範的模式,以及日期和時間元件的符號和名稱。 參數 provider 可以是下列任一項:

如果 providernullDateTimeFormatInfo 則會使用與目前文化特性相關聯的 。 如需詳細資訊,請參閱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