DateTimeOffset.ToString 方法

定義

將目前 DateTimeOffset 物件的值,轉換為其相等的字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation.

多載

ToString()

將目前 DateTimeOffset 物件的值,轉換為其相等的字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation.

ToString(IFormatProvider)

使用指定的文化特性特定格式資訊,將目前 DateTimeOffset 物件的值轉換為其對等字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified culture-specific formatting information.

ToString(String)

使用指定的格式,將目前 DateTimeOffset 物件的值,轉換為其相等字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format.

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性的格式資訊,將目前 DateTimeOffset 物件的值,轉換為其相等的字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format and culture-specific format information.

ToString()

將目前 DateTimeOffset 物件的值,轉換為其相等的字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

包含附加至字串尾端之位移的 DateTimeOffset 物件之字串表示。A string representation of a DateTimeOffset object that includes the offset appended at the end of the string.

例外狀況

日期和時間超出目前文化特性使用之曆法所支援的日期範圍。The date and time is outside the range of dates supported by the calendar used by the current culture.

範例

下列範例說明呼叫 ToString() 方法,並在目前文化特性為 en-us 的系統上顯示其輸出。The following example illustrates calls to the ToString() method and displays its output on a system whose current culture is en-us.

DateTimeOffset thisDate;

// Show output for UTC time
thisDate = DateTimeOffset.UtcNow;
Console.WriteLine(thisDate.ToString());  // Displays 3/28/2007 7:13:50 PM +00:00

// Show output for local time 
thisDate = DateTimeOffset.Now;
Console.WriteLine(thisDate.ToString());  // Displays 3/28/2007 12:13:50 PM -07:00

// Show output for arbitrary time offset
thisDate = thisDate.ToOffset(new TimeSpan(-5, 0, 0));
Console.WriteLine(thisDate.ToString());  // Displays 3/28/2007 2:13:50 PM -05:00
Dim thisDate As DateTimeOffset

' Show output for UTC time
thisDate = DateTimeOffset.UtcNow
Console.WriteLine(thisDate.ToString())  ' Displays 3/28/2007 7:13:50 PM +00:00

' Show output for local time 
thisDate = DateTimeOffset.Now
Console.WriteLine(thisDate.ToString())  ' Displays 3/28/2007 12:13:50 PM -07:00

' Show output for arbitrary time offset
thisDate = thisDate.ToOffset(new TimeSpan(-5, 0, 0))
Console.WriteLine(thisDate.ToString())  ' Displays 3/28/2007 2:13:50 PM -05:00

備註

這個方法的傳回值與 DateTime.ToString() 方法相同,不同之處在于它包含空格,後面接著附加在字串結尾的位移。The return value of this method is identical to that of the DateTime.ToString() method, except that it includes a space followed by the offset appended at the end of the string. 換句話說,它會使用簡短日期模式、長時間模式和 zzz 的自訂格式字串來格式化輸出,其中每個專案都會以空格分隔每個元素。In other words, it formats output using the short date pattern, the long time pattern, and the zzz custom format string, with each element separated from the previous element by a space. 例如,如果 DateTime.ToString() 傳回 1/12/2008 6:15:50 PM 的值,ToString() 會傳回 1/12/2008 6:15:50 PM-08:00 的值,這是國際標準時間(UTC)之後八小時的時間。For example, if DateTime.ToString() returns a value of 1/12/2008 6:15:50 PM, ToString() returns a value of 1/12/2008 6:15:50 PM -08:00 for a time that is eight hours behind Coordinated Universal Time (UTC).

這個方法會使用衍生自目前文化特性的格式資訊。This method uses formatting information derived from the current culture. 如需詳細資訊,請參閱CurrentCultureFor more information, see CurrentCulture. ToString 方法的其他多載可讓您指定要使用其格式的文化特性,以及定義 DateTimeOffset 值的輸出模式。Other overloads of the ToString method enable you to specify the culture whose formatting to use, and to define the output pattern of the DateTimeOffset value.

給呼叫者的注意事項

ToString() 方法會傳回目前文化特性所使用之行事曆中日期和時間的字串表示。The ToString() method returns the string representation of the date and time in the calendar used by the current culture. 如果目前 DateTimeOffset 實例的值早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime,則方法會擲回 ArgumentOutOfRangeExceptionIf the value of the current DateTimeOffset instance is earlier than MinSupportedDateTime or later than MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. 下列範例提供一個實例。The following example provides an illustration. 當目前的文化特性為阿拉伯文(敘利亞)時,它會嘗試格式化超出 HijriCalendar 類別範圍的日期。It attempts to format a date that is outside the range of the HijriCalendar class when the current culture is Arabic (Syria).

[! code-csharpsystem.object # 2][! code-vbsystem.object # 2][!code-csharpSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#2] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#2]

ToString(IFormatProvider)

使用指定的文化特性特定格式資訊,將目前 DateTimeOffset 物件的值轉換為其對等字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified culture-specific formatting information.

public:
 System::String ^ ToString(IFormatProvider ^ formatProvider);
public string ToString (IFormatProvider formatProvider);
override this.ToString : IFormatProvider -> string
Public Function ToString (formatProvider As IFormatProvider) As String

參數

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

目前 DateTimeOffset 物件值的字串表示 (如 formatProvider 所指定)。A string representation of the value of the current DateTimeOffset object, as specified by formatProvider.

例外狀況

日期和時間超出 formatProvider 使用之曆法所支援的日期範圍。The date and time is outside the range of dates supported by the calendar used by formatProvider.

範例

下列範例會使用代表不變文化特性的 CultureInfo 物件,以及其他四個文化特性,來顯示 DateTimeOffset 物件。The following example displays a DateTimeOffset object using CultureInfo objects that represent the invariant culture, as well as four other cultures.

     CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture, 
                                                new CultureInfo("en-us"), 
                                                new CultureInfo("fr-fr"), 
                                                new CultureInfo("de-DE"), 
                                                new CultureInfo("es-ES")};

     DateTimeOffset thisDate = new DateTimeOffset(2007, 5, 1, 9, 0, 0, 
                                                  TimeSpan.Zero);                                            

     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/2007 09:00:00 +00:00
     //    In en-US, 5/1/2007 9:00:00 AM +00:00
     //    In fr-FR, 01/05/2007 09:00:00 +00:00
     //    In de-DE, 01.05.2007 09:00:00 +00:00
     //    In es-ES, 01/05/2007 9:00:00 +00:00
     Dim cultures() As CultureInfo = {CultureInfo.InvariantCulture, _
                                      New CultureInfo("en-us"), _
                                      New CultureInfo("fr-fr"), _
                                      New CultureInfo("de-DE"), _
                                      New CultureInfo("es-ES")}

     Dim thisDate As New DateTimeOffset(#5/1/2007 9:00AM#, TimeSpan.Zero)                                            

     For Each culture As CultureInfo In cultures
        Dim cultureName As String 
        If String.IsNullOrEmpty(culture.Name) Then
           cultureName = culture.NativeName
        Else
           cultureName = culture.Name
        End If
        Console.WriteLine("In {0}, {1}", _
                          cultureName, thisDate.ToString(culture))
     Next                                            
     ' The example produces the following output:
     '    In Invariant Language (Invariant Country), 05/01/2007 09:00:00 +00:00
     '    In en-US, 5/1/2007 9:00:00 AM +00:00
     '    In fr-FR, 01/05/2007 09:00:00 +00:00
     '    In de-DE, 01.05.2007 09:00:00 +00:00
     '    In es-ES, 01/05/2007 9:00:00 +00:00

備註

這個方法的傳回值與其 DateTime.ToString 方法的對等多載相同,不同之處在于它包含空格,後面接著附加在字串結尾的位移。The return value of this method is identical to that of its equivalent overload of the DateTime.ToString method, except that it includes a space followed by the offset appended at the end of the string. 換句話說,它會使用簡短日期模式、長時間模式和 zzz 的自訂格式字串來格式化輸出,其中每個專案都會以空格分隔每個元素。In other words, it formats output using the short date pattern, the long time pattern, and the zzz custom format string, with each element separated from the previous element by a space.

這三個元素的格式是由 formatProvider 參數所定義。The format of these three elements is defined by the formatProvider parameter. formatProvider 參數可以是下列其中一項:The formatProvider parameter can be either of the following:

如果 null``formatProvider,則會使用與目前文化特性相關聯的 DateTimeFormatInfo 物件(請參閱 CurrentCulture)。If formatProvider is null, the DateTimeFormatInfo object associated with the current culture is used (see CurrentCulture).

給呼叫者的注意事項

ToString(IFormatProvider) 方法會傳回行事曆中,由 formatProvider 參數所表示之文化特性所使用之日期和時間的字串表示。The ToString(IFormatProvider) method returns the string representation of the date and time in the calendar used by the culture represented by the formatProvider parameter. 其行事曆是由 Calendar 屬性所定義。Its calendar is defined by the Calendar property. 如果目前 DateTimeOffset 實例的值早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime,則方法會擲回 ArgumentOutOfRangeExceptionIf the value of the current DateTimeOffset instance is earlier than MinSupportedDateTime or later than MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. 下列範例提供一個實例。The following example provides an illustration. 它會嘗試格式化超出 JapaneseCalendar 類別範圍的日期。It attempts to format a date that is outside the range of the JapaneseCalendar class.

[! code-csharpsystem.object # 1][! code-vbsystem.object # 1][!code-csharpSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#1] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#1]

ToString(String)

使用指定的格式,將目前 DateTimeOffset 物件的值,轉換為其相等字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString (string format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

格式字串。A format string.

傳回

目前 DateTimeOffset 物件值的字串表示 (如 format 所指定)。A string representation of the value of the current DateTimeOffset object, as specified by format.

例外狀況

format 長度為 1,而且不屬於針對 DateTimeFormatInfo 定義的標準格式規範字元。The length of format is one, and it is not one of the standard format specifier characters defined for DateTimeFormatInfo.

-或--or- format 不包含有效的自訂格式模式。format does not contain a valid custom format pattern.

日期和時間超出目前文化特性使用之曆法所支援的日期範圍。The date and time is outside the range of dates supported by the calendar used by the current culture.

範例

下列範例會使用每個標準日期和時間格式規範,在主控台中顯示 DateTimeOffset 物件。The following example displays a DateTimeOffset object to the console using each of the standard date and time format specifiers. 輸出會使用 en-us 文化特性來格式化。The output is formatted by using the en-us culture.

DateTimeOffset outputDate = new DateTimeOffset(2007, 10, 31, 21, 0, 0, 
                                     new TimeSpan(-8, 0, 0));
string specifier; 
      
// Output date using each standard date/time format specifier
specifier = "d";
// Displays   d: 10/31/2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "D";
// Displays   D: Wednesday, October 31, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "t";
// Displays   t: 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "T";
// Displays   T: 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "f";
// Displays   f: Wednesday, October 31, 2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "F";
// Displays   F: Wednesday, October 31, 2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "g";
// Displays   g: 10/31/2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "G";
// Displays   G: 10/31/2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "M";           // 'm' is identical
// Displays   M: October 31
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "R";           // 'r' is identical
// Displays   R: Thu, 01 Nov 2007 05:00:00 GMT
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "s";
// Displays   s: 2007-10-31T21:00:00
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

specifier = "u";
// Displays   u: 2007-11-01 05:00:00Z
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 

// Specifier is not supported
specifier = "U";
try
{
   Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier));
}    
catch (FormatException)
{
   Console.WriteLine("{0}: Not supported.", specifier);   
}

specifier = "Y";         // 'y' is identical
// Displays   Y: October, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)); 
Dim outputDate As New DateTimeOffset(#10/31/2007 9:00PM#, _
                                     New TimeSpan(-8, 0, 0))
Dim specifier As String 
      
' Output date using each standard date/time format specifier
specifier = "d"
' Displays   d: 10/31/2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "D"
' Displays   D: Wednesday, October 31, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "t"
' Displays   t: 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "T"
' Displays   T: 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "f"
' Displays   f: Wednesday, October 31, 2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "F"
' Displays   F: Wednesday, October 31, 2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "g"
' Displays   g: 10/31/2007 9:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "G"
' Displays   G: 10/31/2007 9:00:00 PM
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "M"           ' 'm' is identical
' Displays   M: October 31
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "R"           ' 'r' is identical
' Displays   R: Thu, 01 Nov 2007 05:00:00 GMT
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "s"
' Displays   s: 2007-10-31T21:00:00
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

specifier = "u"
' Displays   u: 2007-11-01 05:00:00Z
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

' Specifier is not supported
specifier = "U"
Try
   Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 
Catch e As FormatException
   Console.WriteLine("{0}: Not supported.", specifier)   
End Try

specifier = "Y"         ' 'y' is identical
' Displays   Y: October, 2007
Console.WriteLine("{0}: {1}", specifier, outputDate.ToString(specifier)) 

備註

format 參數應該包含單一格式規範字符(請參閱標準日期和時間格式字串)或自訂格式模式(請參閱自訂日期和時間格式字串),以定義傳回字串的格式。The format parameter should contain either a single format specifier character (see Standard Date and Time Format Strings) or a custom format pattern (see Custom Date and Time Format Strings) that defines the format of the returned string. 如果 format 是 null 或空字串(""),則 DateTimeOffset 值是使用預設格式的輸出。If format is a null or empty string (""), the DateTimeOffset value is output using the default format.

下表顯示與 DateTimeOffset搭配使用時,特定格式規範的確切作業,這與搭配 DateTime使用時的行為不同。The following table shows the exact operation of certain format specifiers when used with DateTimeOffset, which differs from their behavior when used with DateTime.

現有的格式規範Existing format specifier 新的行為New behavior
"K""K" 設計來反復存取日期和時間。Designed to round-trip a date and time. 使用 DateTimeOffset時,會對應至 "zzz" (位移一律以小時和分鐘顯示)。With DateTimeOffset, maps to "zzz" (the offset is always displayed with hours and minutes). 請注意,"K" 是自訂格式規範;它不能在 format中顯示為單一字元。Note that "K" is a custom format specifier; it cannot appear as the single character in format.
"U""U" 不支援。Not supported.
r"r" DateTimeOffset 物件轉換為國際標準時間(UTC),並使用自訂格式字串 ddd, dd MMM yyyy HH:mm:ss GMT輸出。Converts the DateTimeOffset object to Coordinated Universal Time (UTC) and outputs it using the custom format string ddd, dd MMM yyyy HH:mm:ss GMT.
"u""u" DateTimeOffset 物件轉換成 UTC,並使用 yyyy-MM-dd HH:mm:ssZ格式輸出。Converts the DateTimeOffset object to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ.

其餘標準日期和時間格式規範的行為與 ToString(String) 方法相同,與 ToString 方法相同。The remaining standard date and time format specifiers behave the same with the ToString(String) method as they do with the ToString method.

這個方法會使用衍生自目前文化特性的格式資訊。This method uses formatting information derived from the current culture. 如需詳細資訊,請參閱CurrentCultureFor more information, see CurrentCulture.

給呼叫者的注意事項

ToString(String) 方法會傳回目前文化特性所使用之行事曆中日期和時間的字串表示。The ToString(String) method returns the string representation of the date and time in the calendar used by the current culture. 如果目前 DateTimeOffset 實例的值早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime,則方法會擲回 ArgumentOutOfRangeExceptionIf the value of the current DateTimeOffset instance is earlier than MinSupportedDateTime or later than MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. 下列範例提供一個實例。The following example provides an illustration. 當目前的文化特性為希伯來文(以色列)時,它會嘗試格式化超出 HebrewCalendar 類別範圍的日期。It attempts to format a date that is outside the range of the HebrewCalendar class when the current culture is Hebrew (Israel).

[! code-csharpsystem.object # 3][! code-vbsystem.object # 3][!code-csharpSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#3] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#3]

另請參閱

ToString(String, IFormatProvider)

使用指定的格式和特定文化特性的格式資訊,將目前 DateTimeOffset 物件的值,轉換為其相等的字串表示。Converts the value of the current DateTimeOffset object to its equivalent string representation using the specified format and culture-specific format information.

public:
 virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ formatProvider);
public string ToString (string format, IFormatProvider formatProvider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, formatProvider As IFormatProvider) As String

參數

format
String

格式字串。A format string.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

傳回

目前 DateTimeOffset 物件值的字串表示,如 formatprovider 所指定。A string representation of the value of the current DateTimeOffset object, as specified by format and provider.

實作

例外狀況

format 長度為 1,而且不屬於針對 DateTimeFormatInfo 定義的標準格式規範字元。The length of format is one, and it is not one of the standard format specifier characters defined for DateTimeFormatInfo.

-或--or- format 不包含有效的自訂格式模式。format does not contain a valid custom format pattern.

日期和時間超出 formatProvider 使用之曆法所支援的日期範圍。The date and time is outside the range of dates supported by the calendar used by formatProvider.

範例

下列範例會使用 ToString(String, IFormatProvider) 方法,以使用多個不同文化特性的自訂格式字串來顯示 DateTimeOffset 物件。The following example uses the ToString(String, IFormatProvider) method to display a DateTimeOffset object using a custom format string for several different cultures.

DateTimeOffset outputDate = new DateTimeOffset(2007, 11, 1, 9, 0, 0, 
                                     new TimeSpan(-7, 0, 0)); 
string format = "dddd, MMM dd yyyy HH:mm:ss zzz";

// Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, null as DateTimeFormatInfo));
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture));
Console.WriteLine(outputDate.ToString(format, 
                                      new CultureInfo("fr-FR")));
Console.WriteLine(outputDate.ToString(format, 
                                      new CultureInfo("es-ES")));
// The example displays the following output to the console:
//    Thursday, Nov 01 2007 09:00:00 -07:00
//    Thursday, Nov 01 2007 09:00:00 -07:00
//    jeudi, nov. 01 2007 09:00:00 -07:00
//    jueves, nov 01 2007 09:00:00 -07:00
Dim outputDate As New DateTimeOffset(#11/1/2007 9:00AM#, _
                                     New TimeSpan(-7, 0, 0)) 
Dim format As String = "dddd, MMM dd yyyy HH:mm:ss zzz"

' Output date and time using custom format specification
Console.WriteLine(outputDate.ToString(format, Nothing))
Console.WriteLine(outputDate.ToString(format, CultureInfo.InvariantCulture))
Console.WriteLine(outputDate.ToString(format, _
                                      New CultureInfo("fr-FR")))
Console.WriteLine(outputDate.ToString(format, _
                                      New CultureInfo("es-ES")))
' The example displays the following output to the console:
'    Thursday, Nov 01 2007 09:00:00 -07:00
'    Thursday, Nov 01 2007 09:00:00 -07:00
'    jeudi, nov. 01 2007 09:00:00 -07:00
'    jueves, nov 01 2007 09:00:00 -07:00

備註

format 參數應該包含單一格式規範字符(請參閱標準日期和時間格式字串)或自訂格式模式(請參閱自訂日期和時間格式字串)。The format parameter should contain either a single format specifier character (see Standard Date and Time Format Strings) or a custom format pattern (see Custom Date and Time Format Strings). 如果 format 為 null 或空字串(""),則會使用預設格式輸出 DateTimeOffset 物件。If format is a null or empty string (""), the DateTimeOffset object is output using the default format.

下表顯示與 DateTimeOffset搭配使用時,特定格式規範的確切作業,這與搭配 DateTime使用時的行為不同。The following table shows the exact operation of certain format specifiers when used with DateTimeOffset, which differs from their behavior when used with DateTime.

現有的格式規範Existing format specifier 新的行為New behavior
"K""K" 設計來反復存取日期和時間。Designed to round-trip a date and time. 使用 DateTimeOffset時,會對應至 "zzz" (位移一律以小時和分鐘顯示)。With DateTimeOffset, maps to "zzz" (the offset is always displayed with hours and minutes). 請注意,"K" 是自訂格式規範;它不能在 format中顯示為單一字元。Note that "K" is a custom format specifier; it cannot appear as the single character in format.
"U""U" 不支援。Not supported.
r"r" DateTimeOffset 物件轉換為國際標準時間(UTC),並使用自訂格式字串 ddd, dd MMM yyyy HH:mm:ss GMT輸出。Converts the DateTimeOffset object to Coordinated Universal Time (UTC) and outputs it using the custom format string ddd, dd MMM yyyy HH:mm:ss GMT.
"u""u" DateTimeOffset 值轉換成 UTC,並使用 yyyy-MM-dd HH:mm:ssZ格式將其輸出。Converts the DateTimeOffset value to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ.

其餘標準日期和時間格式規範的行為與 ToString(String) 方法相同,與 ToString 方法相同。The remaining standard date and time format specifiers behave the same with the ToString(String) method as they do with the ToString method.

對應至標準格式規範的模式,以及日期和時間元件的符號和名稱,是由 formatProvider 參數所定義。The pattern that corresponds to standard format specifiers, as well as the symbols and names of date and time components, is defined by the formatProvider parameter. formatProvider 參數可以是下列其中一項:The formatProvider parameter can be either of the following:

如果 null``formatProvider,則會使用與目前文化特性相關聯的 DateTimeFormatInfo 物件(請參閱 CurrentCulture)。If formatProvider is null, the DateTimeFormatInfo object associated with the current culture is used (see CurrentCulture).

給呼叫者的注意事項

ToString(String, IFormatProvider) 方法會傳回 formatProvider 參數所使用之行事曆中日期和時間的字串表示。The ToString(String, IFormatProvider) method returns the string representation of the date and time in the calendar used by the formatProvider parameter. 其行事曆是由 Calendar 屬性所定義。Its calendar is defined by the Calendar property. 如果目前 DateTimeOffset 實例的值早于 MinSupportedDateTime 或晚于 MaxSupportedDateTime,則方法會擲回 ArgumentOutOfRangeExceptionIf the value of the current DateTimeOffset instance is earlier than MinSupportedDateTime or later than MaxSupportedDateTime, the method throws an ArgumentOutOfRangeException. 下列範例提供一個實例。The following example provides an illustration. 它會嘗試格式化超出 UmAlQuraCalendar 類別範圍的日期。It attempts to format a date that is outside the range of the UmAlQuraCalendar class.

[! code-csharpsystem.object # 4][! code-vbsystem.object # 4][!code-csharpSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#4] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#4]

另請參閱

適用於