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

傳回

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 的值, 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 讓您指定要使用其格式的文化特性,以及定義值的輸出模式 DateTimeOffsetOther 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).

[!程式碼-csharp system.string.ToString. ArgumentOutOfRangeException # 2][!程式碼-vb system.servicemodel.ToString. ArgumentOutOfRangeException # 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);
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.

傳回

String

目前 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.

範例

下列範例 DateTimeOffset 會使用代表不因 CultureInfo 文化特性而異的物件,以及其他四種文化特性,來顯示物件。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.

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

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

給呼叫者的注意事項

方法會傳回由 ToString(IFormatProvider) 參數所代表之文化特性所使用行事曆中日期和時間的字串表示 formatProviderThe 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. 它會嘗試將類別範圍以外的日期格式化 JapaneseCalendarIt attempts to format a date that is outside the range of the JapaneseCalendar class.

::: code language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception1.cs" interactive = "try-dotnet" id = "Snippet1":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception1.vb" id = "Snippet1"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception1.cs" interactive="try-dotnet" id="Snippet1"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception1.vb" id="Snippet1":::

適用於

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);
public string ToString (string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

參數

format
String

格式字串。A format string.

傳回

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 使用時的行為不同 DateTimeThe 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" 是自訂格式規範;它不能顯示為中的單一字元 formatNote that "K" is a custom format specifier; it cannot appear as the single character in format.
"U""U" 不支援。Not supported.
桌上型電腦主機板"r" DateTimeOffset 物件轉換為國際標準時間 (UTC) 並使用自訂格式字串來輸出 ddd, dd MMM yyyy HH:mm:ss GMTConverts 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:ssZConverts the DateTimeOffset object to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ.

其餘標準日期和時間格式規範的行為與方法相同 ToString(String) ToStringThe 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 language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception3.cs" interactive = "try-dotnet" id = "Snippet3":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception3.vb" id = "Snippet3"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception3.cs" interactive="try-dotnet" id="Snippet3"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception3.vb" id="Snippet3":::

另請參閱

適用於

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);
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.

傳回

String

目前 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 使用時的行為不同 DateTimeThe 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" 是自訂格式規範;它不能顯示為中的單一字元 formatNote that "K" is a custom format specifier; it cannot appear as the single character in format.
"U""U" 不支援。Not supported.
桌上型電腦主機板"r" DateTimeOffset 物件轉換為國際標準時間 (UTC) 並使用自訂格式字串來輸出 ddd, dd MMM yyyy HH:mm:ss GMTConverts 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:ssZConverts the DateTimeOffset value to UTC and outputs it using the format yyyy-MM-dd HH:mm:ssZ.

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

對應至標準格式規範的模式,以及日期和時間元件的符號和名稱,是由參數所定義 formatProviderThe 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:

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

給呼叫者的注意事項

方法會傳回 ToString(String, IFormatProvider) 參數所使用行事曆中日期和時間的字串表示 formatProviderThe 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. 它會嘗試將類別範圍以外的日期格式化 UmAlQuraCalendarIt attempts to format a date that is outside the range of the UmAlQuraCalendar class.

::: code language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception4.cs" id = "Snippet4":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception4.vb" id = "Snippet4"::::::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/cs/datetimeoffset.tostring.argumentoutofrangeexception4.cs" id="Snippet4"::: :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.datetimeoffset.tostring.argumentoutofrangeexception/vb/datetimeoffset.tostring.argumentoutofrangeexception4.vb" id="Snippet4":::

另請參閱

適用於