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.DateTimeOffset.ToString.ArgumentOutOfRangeException#2] [!code-vbSystem.DateTimeOffset.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);
override this.ToString : IFormatProvider -> string
Public Function ToString (formatProvider As IFormatProvider) As String

参数

formatProvider
IFormatProvider

一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.

返回

formatProvider 指定的当前 DateTimeOffset 对象值的字符串表示形式。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.DateTimeOffset.ToString.ArgumentOutOfRangeException#1] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#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.

返回

format 指定的当前 DateTimeOffset 对象值的字符串表示形式。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" 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.DateTimeOffset.ToString.ArgumentOutOfRangeException#3] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#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" 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.DateTimeOffset.ToString.ArgumentOutOfRangeException#4] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#4][!code-csharpSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#4] [!code-vbSystem.DateTimeOffset.ToString.ArgumentOutOfRangeException#4]

另请参阅

适用于