DateTimeOffset.ParseExact 方法

定义

将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match a specified format exactly.

重载

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. 字符串表示形式的格式必须与一种指定的格式完全匹配。The format of the string representation must match one of the specified formats exactly.

ParseExact(String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified format, culture-specific format information, and style. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match the specified format exactly.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将表示日期和时间的字符范围转换为其等效的 DateTimeOffsetConverts a character span that represents a date and time to its DateTimeOffset equivalent using the specified format, culture-specific format information, and style. 日期和时间表示形式的格式必须与指定的格式完全匹配。The format of the date and time representation must match the specified format exactly.

ParseExact(String, String, IFormatProvider)

使用指定的格式和区域性特定格式信息,将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified format and culture-specific format information. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match the specified format exactly.

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将包含日期和时间的字符串表示形式的字符范围转换为其等效的 DateTimeOffsetConverts a character span that contains the string representation of a date and time to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. 日期和时间表示形式的格式必须与一种指定的格式完全匹配。The format of the date and time representation must match one of the specified formats exactly.

ParseExact(String, String[], IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. 字符串表示形式的格式必须与一种指定的格式完全匹配。The format of the string representation must match one of the specified formats exactly.

public:
 static DateTimeOffset ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles);
public static DateTimeOffset ParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset

参数

input
String

包含要转换的日期和时间的字符串。A string that contains a date and time to convert.

formats
String[]

一个由格式说明符组成的数组,格式说明符用于定义 input 的期望格式。An array of format specifiers that define the expected formats of input.

formatProvider
IFormatProvider

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

styles
DateTimeStyles

枚举值的一个按位组合,指示 input 所允许的格式。A bitwise combination of enumeration values that indicates the permitted format of input.

返回

一个对象,它等效于 input 参数中包含的日期和时间,由 formatsformatProviderstyles 参数指定。An object that is equivalent to the date and time that is contained in the input parameter, as specified by the formats, formatProvider, and styles parameters.

例外

偏移量大于 14 小时或小于 -14 小时。The offset is greater than 14 hours or less than -14 hours.

- 或 --or- styles 包含不支持的值。styles includes an unsupported value.

- 或 --or- styles 参数包含不能一起使用的 DateTimeStyles 值。The styles parameter contains DateTimeStyles values that cannot be used together.

inputnullinput is null.

input 为空字符串 ("")。input is an empty string ("").

- 或 --or- input 不包含日期和时间的有效字符串表示形式。input does not contain a valid string representation of a date and time.

- 或 --or- formats 的元素均不包含有效的格式说明符。No element of formats contains a valid format specifier.

- 或 --or- input 中的小时组件和 AM/PM 指示符不一致。The hour component and the AM/PM designator in input do not agree.

示例

下面的示例为日期和时间和偏移量值的字符串表示形式定义多个输入格式,然后将用户输入的字符串传递给 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 方法。The following example defines multiple input formats for the string representation of a date and time and offset value, and then passes the string that is entered by the user to the DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method.

 TextReader conIn = Console.In;
 TextWriter conOut = Console.Out;
 int tries = 0;
 string input = String.Empty;
 string[] formats = new string[] {@"@M/dd/yyyy HH:m zzz", @"MM/dd/yyyy HH:m zzz", 
                                  @"M/d/yyyy HH:m zzz", @"MM/d/yyyy HH:m zzz", 
                                  @"M/dd/yy HH:m zzz", @"MM/dd/yy HH:m zzz", 
                                  @"M/d/yy HH:m zzz", @"MM/d/yy HH:m zzz",                             
                                  @"M/dd/yyyy H:m zzz", @"MM/dd/yyyy H:m zzz", 
                                  @"M/d/yyyy H:m zzz", @"MM/d/yyyy H:m zzz", 
                                  @"M/dd/yy H:m zzz", @"MM/dd/yy H:m zzz", 
                                  @"M/d/yy H:m zzz", @"MM/d/yy H:m zzz",                               
                                  @"M/dd/yyyy HH:mm zzz", @"MM/dd/yyyy HH:mm zzz", 
                                  @"M/d/yyyy HH:mm zzz", @"MM/d/yyyy HH:mm zzz", 
                                  @"M/dd/yy HH:mm zzz", @"MM/dd/yy HH:mm zzz", 
                                  @"M/d/yy HH:mm zzz", @"MM/d/yy HH:mm zzz",                                 
                                  @"M/dd/yyyy H:mm zzz", @"MM/dd/yyyy H:mm zzz", 
                                  @"M/d/yyyy H:mm zzz", @"MM/d/yyyy H:mm zzz", 
                                  @"M/dd/yy H:mm zzz", @"MM/dd/yy H:mm zzz", 
                                  @"M/d/yy H:mm zzz", @"MM/d/yy H:mm zzz"};
 IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
 DateTimeOffset result = new DateTimeOffset();

 do { 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),");
    conOut.Write("Then press Enter: ");
    input = conIn.ReadLine();
    conOut.WriteLine();
    try
    {
       result = DateTimeOffset.ParseExact(input, formats, provider, 
                                          DateTimeStyles.AllowWhiteSpaces);
       break;
    }
    catch (FormatException)
    {
       Console.WriteLine("Unable to parse {0}.", input);      
       tries++;
    }
 } while (tries < 3);
 if (tries >= 3)
    Console.WriteLine("Exiting application without parsing {0}", input);
 else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString());                                                     
 // Some successful sample interactions with the user might appear as follows:
 //    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 //    Then press Enter: 12/08/2007 6:54 -6:00
 //    
 //    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 //    
 //    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 //    Then press Enter: 12/8/2007 06:54 -06:00
 //    
 //    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 //    
 //    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 //    Then press Enter: 12/5/07 6:54 -6:00
 //    
 //    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00 
 Dim conIn As TextReader = Console.In
 Dim conOut As TextWriter = Console.Out
 Dim tries As Integer = 0
 Dim input As String = String.Empty
 Dim formats() As String = {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", _
                            "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", _
                            "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", _
                            "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz", _                                 
                            "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", _
                            "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", _
                            "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", _
                            "M/d/yy H:m zzz", "MM/d/yy H:m zzz", _                               
                            "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", _
                            "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", _
                            "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", _
                            "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz", _                                 
                            "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", _
                            "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", _
                            "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", _
                            "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"}   
 Dim provider As IFormatProvider = CultureInfo.InvariantCulture.DateTimeFormat
 Dim result As DateTimeOffset

 Do 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),")
    conOut.Write("Then press Enter: ")
    input = conIn.ReadLine()
    conOut.WriteLine() 
    Try
       result = DateTimeOffset.ParseExact(input, formats, provider, _
                                          DateTimeStyles.AllowWhiteSpaces)
       Exit Do
    Catch e As FormatException
       Console.WriteLine("Unable to parse {0}.", input)      
       tries += 1
    End Try
 Loop While tries < 3
 If tries >= 3 Then
    Console.WriteLine("Exiting application without parsing {0}", input)
 Else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString())                                                     
 End If 
 ' Some successful sample interactions with the user might appear as follows:
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/08/2007 6:54 -6:00
 '    
 '    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/8/2007 06:54 -06:00
 '    
 '    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/5/07 6:54 -6:00
 '    
 '    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00 

注解

DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 方法分析与分配给 formats 参数的模式之一匹配的日期的字符串表示形式。The DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method parses the string representation of a date that matches any one of the patterns assigned to the formats parameter. 如果 input 字符串 styles 与上述任意一种模式都不匹配,则该方法将引发一个 FormatExceptionIf the input string does not match any one of these patterns with any of the variations defined by the styles parameter, the method throws a FormatException. 除了将 input 与多个格式设置模式进行比较以外,此重载的行为与 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法相同。Aside from comparing input to multiple formatting patterns, this overload behaves identically to the DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) method.

formats 参数是一个字符串数组,其元素包含单个标准格式说明符,或者包含一个或多个定义 input 参数的可能模式的自定义格式说明符。The formats parameter is a string array whose elements contain either a single standard format specifier or one or more custom format specifiers that define the possible pattern of the input parameter. 调用方法时,input 必须匹配其中一种模式。When the method is called, input must match one of these patterns. 有关有效格式设置代码的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. 如果 formats 中的匹配的元素包括 zzzzzz 自定义格式说明符,以指示在 input中必须存在偏移量,则该偏移量必须包括负号或正负号。If the matched element in formats includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. 如果缺少符号,则方法会引发 FormatExceptionIf the sign is missing, the method throws a FormatException.

重要

使用此重载的 formats 参数指定多种格式可帮助减少许多用户在输入日期和时间时遇到的挫折。Using the formats parameter of this overload to specify multiple formats can help reduce the frustration many users experience when they enter dates and times. 特别是,定义多个输入模式的功能使应用程序能够处理日期和时间表示形式,该表示形式可以包括或缺少前导零(以月、天、小时、分钟和秒为单位)。In particular, the ability to define multiple input patterns enables an application to handle date and time representations that can either include or lack leading zeros in months, days, hours, minutes, and seconds. 该示例提供了此的说明。The example provides an illustration of this.

如果 formats 中的匹配元素要求 input 包含日期但不包含时间,则生成的 DateTimeOffset 对象的时间为午夜(0:00:00)。If the matched element in formats requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). 如果 formats 中的匹配元素要求 input 包含时间而不包含日期,则生成的 DateTimeOffset 对象将被分配到本地系统上的当前日期。If the matched element in formats requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. 如果 formats 中的匹配元素不要求 input 包含偏移量,则生成的 DateTimeOffset 对象的偏移量取决于 styles 参数的值。If the matched element in formats does not require that input contain an offset, the offset of the resulting DateTimeOffset object depends on the value of the styles parameter. 如果 styles 包括 AssumeLocal,则会将本地时区的偏移量分配给 DateTimeOffset 对象。If styles includes AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. 如果 styles 包括 AssumeUniversal,则会将协调世界时(UTC)偏移量或 + 00:00 分配给 DateTimeOffset 对象。If styles includes AssumeUniversal, the Coordinated Universal Time (UTC) offset, or +00:00, is assigned to the DateTimeOffset object. 如果这两个值均未指定,则使用本地时区的偏移量。If neither value is specified, the offset of the local time zone is used.

input 中使用的特定日期和时间符号和字符串由 formatProvider 参数定义。The particular date and time symbols and strings used in input are defined by the formatProvider parameter. 如果 formats 的匹配元素是标准格式说明符字符串,则 input的精确格式也是如此。The same is true for the precise format of input, if the matching element of formats is a standard format specifier string. formatProvider 参数可以是以下项之一:The formatProvider parameter can be either of the following:

如果 null``formatprovider,则使用与当前区域性对应的 CultureInfo 对象。If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

styles 参数定义在输入字符串中是否允许空格,指示如何分析没有显式偏移量组件的字符串,并支持在分析操作中进行 UTC 转换。The styles parameter defines whether white space is permitted in the input string, indicates how strings without an explicit offset component are parsed, and supports UTC conversion as part of the parsing operation. NoCurrentDateDefault之外,还支持 DateTimeStyles 枚举的所有成员。All members of the DateTimeStyles enumeration are supported except NoCurrentDateDefault. 下表列出了每个受支持的成员的效果。The following table lists the effect of each supported member.

DateTimeStyles 成员DateTimeStyles member 行为Behavior
AdjustToUniversal 分析 input,并在必要时将其转换为 UTC。Parses input and, if necessary, converts it to UTC. 它等效于分析字符串,然后调用返回的 DateTimeOffset 对象的 DateTimeOffset.ToUniversalTime 方法。It is equivalent to parsing a string, and then calling the DateTimeOffset.ToUniversalTime method of the returned DateTimeOffset object.
AssumeLocal 如果 formats 中的匹配元素不要求 input 包含 offset 值,则将为返回的 DateTimeOffset 对象提供本地时区的偏移量。If the matched element in formats does not require that input contain an offset value, the returned DateTimeOffset object is given the offset of the local time zone. 这是默认值。This is the default value.
AssumeUniversal 如果 formats 中的匹配元素不要求 input 包含 offset 值,则将为返回的 DateTimeOffset 对象提供 UTC 偏移量(+ 00:00)。If the matched element in formats does not require that input contain an offset value, the returned DateTimeOffset object is given the UTC offset (+00:00).
AllowInnerWhite 允许 input 包括 format未指定的内部空格。Allows input to include inner white space not specified by format. 在对字符串进行分析时,日期和时间部分之间以及单个组件中可能出现额外的空白。Extra white space can appear between date and time components and within individual components (except the offset), and is ignored when parsing the string.
AllowLeadingWhite 允许 input 包括 formats未指定的前导空格。Allows input to include leading spaces not specified by formats. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowTrailingWhite 允许 input 包括 formats未指定的尾随空格。Allows input to include trailing spaces not specified by formats. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowWhiteSpaces 允许 input 包含 formats未指定的前导空格、尾随空格和内部空格。Allows input to include leading, trailing, and inner spaces not specified by formats. 分析字符串时,将忽略在 formats 中的匹配元素中未指定的所有额外空白字符。All extra white-space characters not specified in the matched element in formats are ignored when parsing the string.
None 指示 input中不允许使用额外的空白。Indicates that additional white space is not permitted in input. 空格必须完全按照 formats 的特定元素中的指定显示,才能发生匹配。White space must appear exactly as specified in a particular element of formats for a match to occur. 这是默认行为。This is the default behavior.
RoundtripKind 不起作用,因为 DateTimeOffset 结构不包括 Kind 属性。Has no effect because the DateTimeOffset structure does not include a Kind property.

调用方说明

.NET Framework 4.NET Framework 4中,如果要分析的字符串包含一个小时部分和一个不在协议中的 AM/PM 指示符,则 ParseExact 方法将引发一个 FormatExceptionIn the .NET Framework 4.NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. .NET Framework 3.5.NET Framework 3.5 及更早版本中,将忽略 AM/PM 指示符。In the .NET Framework 3.5.NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

另请参阅

ParseExact(String, String, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified format, culture-specific format information, and style. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match the specified format exactly.

public:
 static DateTimeOffset ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles);
public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles);
static member ParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset

参数

input
String

包含要转换的日期和时间的字符串。A string that contains a date and time to convert.

format
String

用于定义所需的 input 格式的格式说明符。A format specifier that defines the expected format of input.

formatProvider
IFormatProvider

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

styles
DateTimeStyles

枚举值的一个按位组合,指示 input 所允许的格式。A bitwise combination of enumeration values that indicates the permitted format of input.

返回

一个对象,它等效于 input 参数中包含的日期和时间,由 formatformatProviderstyles 参数指定。An object that is equivalent to the date and time that is contained in the input parameter, as specified by the format, formatProvider, and styles parameters.

例外

偏移量大于 14 小时或小于 -14 小时。The offset is greater than 14 hours or less than -14 hours.

- 或 --or- styles 参数包含不支持的值。The styles parameter includes an unsupported value.

- 或 --or- styles 参数包含不能一起使用的 DateTimeStyles 值。The styles parameter contains DateTimeStyles values that cannot be used together.

inputnullinput is null.

- 或 --or- formatnullformat is null.

input 为空字符串 ("")。input is an empty string ("").

- 或 --or- input 不包含日期和时间的有效字符串表示形式。input does not contain a valid string representation of a date and time.

- 或 --or- format 是一个空字符串。format is an empty string.

- 或 --or- input 中的小时组件和 AM/PM 指示符不一致。The hour component and the AM/PM designator in input do not agree.

示例

下面的示例将 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法与标准和自定义格式说明符、固定区域性以及各种 DateTimeStyles 值一起使用来分析多个日期和时间字符串。The following example uses the DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) method with standard and custom format specifiers, the invariant culture, and various DateTimeStyles values to parse several date and time strings.

string dateString, format;  
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008";
format = "d";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider, 
                                      DateTimeStyles.AssumeUniversal);
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("'{0}' is not in the correct format.", dateString);
} 

// Parse date-only value with leading white space.
// Should throw a FormatException because only trailing white space is  
// specified in method call.
dateString = " 06/15/2008";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider, 
                                      DateTimeStyles.AllowTrailingWhite);
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}   
catch (FormatException)
{
   Console.WriteLine("'{0}' is not in the correct format.", dateString);
} 

// Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00";
format = "MM/dd/yyyy H:mm zzz";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider, 
                                      DateTimeStyles.AllowWhiteSpaces);
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}   
catch (FormatException)
{
   Console.WriteLine("'{0}' is not in the correct format.", dateString);
} 

// Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00";
format = "MM/dd/yyyy H:mm:ss zzz"; 
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider, 
                                      DateTimeStyles.AllowWhiteSpaces |
                                      DateTimeStyles.AdjustToUniversal);
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("'{0}' is not in the correct format.", dateString);
} 
// The example displays the following output:
//    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
Dim dateString, format As String  
Dim result As DateTimeOffset
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008"
format = "d"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider, _
                                      DateTimeStyles.AssumeUniversal)
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End Try 

' Parse date-only value with leading white space.
' Should throw a FormatException because only trailing white space is  
' specified in method call.
dateString = " 06/15/2008"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider, _
                                      DateTimeStyles.AllowTrailingWhite)
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End Try 

' Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00"
format = "MM/dd/yyyy H:mm zzz"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider, _
                                      DateTimeStyles.AllowWhiteSpaces)
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End Try 

' Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00"   
format = "MM/dd/yyyy H:mm:ss zzz"       
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider, _
                                      DateTimeStyles.AllowWhiteSpaces Or _
                                      DateTimeStyles.AdjustToUniversal)
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End Try 
' The example displays the following output:
'    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
'    ' 06/15/2008' is not in the correct format.
'    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
'    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.

下面的示例使用各种 DateTimeStyles 值来分析应符合ISO 8601的字符串数组。The following example uses a variety of DateTimeStyles values to parse an array of strings that are expected to conform to ISO 8601. 如示例的输出所示,格式正确的字符串在以下情况下无法分析:As the output from the example shows, strings that are in the proper format fail to parse if:

除非在方法调用中提供了 DateTimeStyles.AssumeUniversal 标志,否则不指定 UTC 偏移量的字符串将采用本地时区的偏移量(在本例中为-07:00)。Strings that do not specify a UTC offset are assumed to have the offset of the local time zone (in this case, -07:00) unless the DateTimeStyles.AssumeUniversal flag is supplied in the method call. 在这种情况下,假定它们是协调世界时。In that case, they are assumed to be Universal Coordinated Time.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "2018-08-18T12:45:16.0000000Z",
                               "2018/08/18T12:45:16.0000000Z",
                               "2018-18-08T12:45:16.0000000Z",
                               "2018-08-18T12:45:16.0000000",                               
                               " 2018-08-18T12:45:16.0000000Z ",
                               "2018-08-18T12:45:16.0000000+02:00",
                               "2018-08-18T12:45:16.0000000-07:00" }; 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal);   }

   private static void ParseWithISO8601(string[] dateStrings, DateTimeStyles styles)
   {   
      Console.WriteLine($"Parsing with {styles}:");
      foreach (var dateString in dateStrings)
      {
         try {
            var date = DateTimeOffset.ParseExact(dateString, "O", null, styles);
            Console.WriteLine($"   {dateString,-35} --> {date:yyyy-MM-dd HH:mm:ss.FF zzz}");
         }
         catch (FormatException)
         {
            Console.WriteLine($"   FormatException: Unable to convert '{dateString}'");
         }   
      } 
   }
}
// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//         FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//         FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//         FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//         FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//         FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim dateStrings() = { "2018-08-18T12:45:16.0000000Z",
                            "2018/08/18T12:45:16.0000000Z",
                            "2018-18-08T12:45:16.0000000Z",
                            "2018-08-18T12:45:16.0000000",                               
                            " 2018-08-18T12:45:16.0000000Z ",
                            "2018-08-18T12:45:16.0000000+02:00",
                            "2018-08-18T12:45:16.0000000-07:00" } 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal)   
   End Sub

   Private Sub ParseWithISO8601(dateStrings() As String, styles As DateTimeStyles)
      Console.WriteLine($"Parsing with {styles}:")
      For Each dateStr In dateStrings
         Try 
            Dim dat = DateTimeOffset.ParseExact(dateString, "O", Nothing, styles)
            Console.WriteLine($"   {dateString,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         catch e As FormatException
            Console.WriteLine($"   FormatException: Unable to convert '{dateString}'")
         End Try   
      Next 
   End Sub
End Module
' The example displays the following output:
'      Parsing with None:
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'
'      -----
'
'      Parsing with AllowWhiteSpaces:
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'
'      -----
'
'      Parsing with AdjustToUniversal:
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'
'      -----
'
'      Parsing with AssumeLocal:
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'
'      -----
'
'      Parsing with AssumeUniversal:
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'
'         FormatException: Unable to convert '07-30-2018'

注解

DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 方法分析日期的字符串表示形式,该日期必须采用 format 参数定义的格式。The DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) method parses the string representation of a date, which must be in the format defined by the format parameter. 它还要求日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移 > 元素按 format指定的顺序出现。It also requires that the <Date>, <Time>, and <Offset> elements of the string representation of a date and time appear in the order specified by format. 如果 input 字符串与 format 参数的模式不匹配,并且具有 styles 参数定义的任何变体,则该方法将引发 FormatExceptionIf the input string does not match the pattern of the format parameter, with any variations defined by the styles parameter, the method throws a FormatException. 与此相反,DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 方法会分析格式提供程序的 DateTimeFormatInfo 对象识别的任何格式的日期的字符串表示形式。In contrast, the DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) method parses the string representation of a date in any one of the formats recognized by the format provider's DateTimeFormatInfo object. Parse 还允许以任意顺序显示日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移量。Parse also allows the <Date>, <Time>, and <Offset> elements of the string representation of a date and time to appear in any order.

format 参数是包含单个标准格式说明符的字符串,或者是一个或多个定义 input 参数所需模式的自定义格式说明符。The format parameter is a string that contains either a single standard format specifier or one or more custom format specifiers that define the required pattern of the input parameter. 有关有效格式设置代码的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. 如果 format 包括 zzzzzz 自定义格式说明符,以指示在 input中必须存在偏移量,则该偏移必须包含负号或正号。If format includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. 如果缺少符号,则方法会引发 FormatExceptionIf the sign is missing, the method throws a FormatException.

如果 format 要求 input 包含日期但不包含时间,则生成的 DateTimeOffset 对象的时间为午夜(0:00:00)。If format requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). 如果 format 要求 input 包含时间而不包含日期,则生成的 DateTimeOffset 对象将被分配到本地系统上的当前日期。If format requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. 如果 format 不要求 input 包含偏移量,则生成的 DateTimeOffset 对象的偏移量取决于 styles 参数的值。If format does not require that input contain an offset, the offset of the resulting DateTimeOffset object depends on the value of the styles parameter. 如果 styles 包括 AssumeLocal,则会将本地时区的偏移量分配给 DateTimeOffset 对象。If styles includes AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. 如果 styles 包括 AssumeUniversal,则会将协调世界时(UTC)偏移量或 + 00:00 分配给 DateTimeOffset 对象。If styles includes AssumeUniversal, the Coordinated Universal Time (UTC) offset, or +00:00, is assigned to the DateTimeOffset object. 如果这两个值均未指定,则使用本地时区的偏移量。If neither value is specified, the offset of the local time zone is used.

input 中使用的特定日期和时间符号和字符串由 formatProvider 参数定义。The particular date and time symbols and strings used in input are defined by the formatProvider parameter. 如果 format 是标准格式说明符字符串,则 input的精确格式也是如此。The same is true for the precise format of input, if format is a standard format specifier string. formatProvider 参数可以是以下项之一:The formatProvider parameter can be either of the following:

如果 null``formatprovider,则使用与当前区域性对应的 CultureInfo 对象。If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

styles 参数定义在输入字符串中是否允许空格,指示如何分析没有显式偏移量组件的字符串,并支持在分析操作中进行 UTC 转换。The styles parameter defines whether white space is allowed in the input string, indicates how strings without an explicit offset component are parsed, and supports UTC conversion as part of the parsing operation. NoCurrentDateDefault之外,还支持 DateTimeStyles 枚举的所有成员。All members of the DateTimeStyles enumeration are supported except NoCurrentDateDefault. 下表列出了每个受支持的成员的效果。The following table lists the effect of each supported member.

DateTimeStyles 成员DateTimeStyles member 行为Behavior
AdjustToUniversal 分析 input,并在必要时将其转换为 UTC。Parses input and, if necessary, converts it to UTC. 它等效于分析字符串,然后调用返回的 DateTimeOffset 对象的 DateTimeOffset.ToUniversalTime 方法。It is equivalent to parsing a string, and then calling the DateTimeOffset.ToUniversalTime method of the returned DateTimeOffset object.
AssumeLocal 如果 format 不要求 input 包含 offset 值,则会为返回的 DateTimeOffset 对象提供本地时区的偏移量。If format does not require that input contain an offset value, the returned DateTimeOffset object is given the offset of the local time zone. 这是默认值。This is the default value.
AssumeUniversal 如果 format 不要求 input 包含 offset 值,则会为返回的 DateTimeOffset 对象提供 UTC 偏移量(+ 00:00)。If format does not require that input contain an offset value, the returned DateTimeOffset object is given the UTC offset (+00:00).
AllowInnerWhite 允许 input 包括 format未指定的内部空格。Allows input to include inner white space not specified by format. 日期和时间部分之间以及单个组件中可能出现额外空白,分析字符串时将忽略此空格。Extra white space can appear between date and time components and within individual components, and is ignored when parsing the string.
AllowLeadingWhite 允许 input 包括 format未指定的前导空格。Allows input to include leading spaces not specified by format. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowTrailingWhite 允许 input 包括 format未指定的尾随空格。Allows input to include trailing spaces not specified by format. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowWhiteSpaces 允许 input 包含 format未指定的前导空格、尾随空格和内部空格。Allows input to include leading, trailing, and inner spaces not specified by format. 分析字符串时,将忽略 format 中未指定的所有额外空白字符。All extra white-space characters not specified in format are ignored when parsing the string.
None 指示 input中不允许使用额外的空白。Indicates that additional white space is not permitted in input. 空格必须完全按照 format中指定的形式出现。White space must appear exactly as specified in format. 这是默认行为。This is the default behavior.
RoundtripKind 不起作用,因为 DateTimeOffset 结构不包括 Kind 属性。Has no effect because the DateTimeOffset structure does not include a Kind property.

调用方说明

.NET Framework 4.NET Framework 4中,如果要分析的字符串包含一个小时部分和一个不在协议中的 AM/PM 指示符,则 ParseExact 方法将引发一个 FormatExceptionIn the .NET Framework 4.NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. .NET Framework 3.5.NET Framework 3.5 及更早版本中,将忽略 AM/PM 指示符。In the .NET Framework 3.5.NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

另请参阅

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将表示日期和时间的字符范围转换为其等效的 DateTimeOffsetConverts a character span that represents a date and time to its DateTimeOffset equivalent using the specified format, culture-specific format information, and style. 日期和时间表示形式的格式必须与指定的格式完全匹配。The format of the date and time representation must match the specified format exactly.

public static DateTimeOffset ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, Optional styles As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTimeOffset

参数

input
ReadOnlySpan<Char>

一个表示日期和时间的字符范围。A character span that represents a date and time.

format
ReadOnlySpan<Char>

包含格式说明符的字符范围,用于定义 input 的预期格式。A character span that contains a format specifier that defines the expected format of input.

formatProvider
IFormatProvider

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

styles
DateTimeStyles

枚举值的一个按位组合,指示 input 所允许的格式。A bitwise combination of enumeration values that indicates the permitted format of input.

返回

一个对象,它等效于 input 参数中包含的日期和时间,由 formatformatProviderstyles 参数指定。An object that is equivalent to the date and time that is contained in the input parameter, as specified by the format, formatProvider, and styles parameters.

例外

偏移量大于 14 小时或小于 -14 小时。The offset is greater than 14 hours or less than -14 hours.
- 或 --or-
styles 参数包含不支持的值。The styles parameter includes an unsupported value.
- 或 --or-
styles 参数包含不能一起使用的 DateTimeStyles 值。The styles parameter contains DateTimeStyles values that cannot be used together.

input 是一个空的字符范围。input is an empty character span.
-或- input 不包含日期和时间的有效字符串表示形式。-or- input does not contain a valid string representation of a date and time.
-或- format 是一个空的字符范围。-or- format is an empty character span.
- 或 --or-
input 中的小时组件和 AM/PM 指示符不一致。The hour component and the AM/PM designator in input do not agree.

注解

此方法分析表示日期的字符跨度,该日期必须采用 format 参数所定义的格式。This method parses a character span that represents a date, which must be in the format defined by the format parameter. 它还要求日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移 > 元素按 format指定的顺序出现。It also requires that the <Date>, <Time>, and <Offset> elements of the string representation of a date and time appear in the order specified by format. 如果 inputformat 模式不匹配,则该方法将引发一个 FormatExceptionIf input does not match the format pattern, the method throws a FormatException. 与此相反,DateTimeOffset.Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 方法会分析格式提供程序的 DateTimeFormatInfo 对象识别的任何格式的日期的字符串表示形式。In contrast, the DateTimeOffset.Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) method parses the string representation of a date in any one of the formats recognized by the format provider's DateTimeFormatInfo object. Parse 还允许以任意顺序显示日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移量。Parse also allows the <Date>, <Time>, and <Offset> elements of the string representation of a date and time to appear in any order.

format 参数是一个字符跨度,其中包含单字符标准格式说明符,或者包含一个或多个定义 input 参数所需格式的自定义格式说明符。The format parameter is a character span that contains either a single-character standard format specifier or one or more custom format specifiers that define the required format of the input parameter. 有关有效格式设置代码的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. 如果 format 包括 zzzzzz 自定义格式说明符,以指示在 input中必须存在偏移量,则该偏移必须包含负号或正号。If format includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. 如果缺少符号,则方法会引发 FormatExceptionIf the sign is missing, the method throws a FormatException.

如果 format 要求 input 包含日期但不包含时间,则生成的 DateTimeOffset 对象的时间为午夜(0:00:00)。If format requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). 如果 format 要求 input 包含时间而不包含日期,则生成的 DateTimeOffset 对象将被分配到本地系统上的当前日期。If format requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. 如果 format 不要求 input 包含偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。If format does not require that input contain an offset, the resulting DateTimeOffset object is assigned the time zone offset of the local system.

input 中使用的特定日期和时间符号和字符串由 formatProvider 参数定义,如果 format 是标准格式说明符,则它是 input 的精确格式。The particular date and time symbols and strings used in input are defined by the formatProvider parameter, as is the precise format of input if format is a standard format specifier. formatProvider 参数可以是以下项之一:The formatProvider parameter can be either of the following:

如果 null``formatprovider,则使用与当前区域性对应的 CultureInfo 对象。If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

ParseExact(String, String, IFormatProvider)

使用指定的格式和区域性特定格式信息,将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffsetConverts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified format and culture-specific format information. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match the specified format exactly.

public:
 static DateTimeOffset ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);
public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider);
static member ParseExact : string * string * IFormatProvider -> DateTimeOffset
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As DateTimeOffset

参数

input
String

包含要转换的日期和时间的字符串。A string that contains a date and time to convert.

format
String

用于定义所需的 input 格式的格式说明符。A format specifier that defines the expected format of input.

formatProvider
IFormatProvider

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

返回

一个对象,它等效于 input 中包含的日期和时间,由 formatformatProvider 指定。An object that is equivalent to the date and time that is contained in input as specified by format and formatProvider.

例外

偏移量大于 14 小时或小于 -14 小时。The offset is greater than 14 hours or less than -14 hours.

inputnullinput is null.

- 或 --or- formatnullformat is null.

input 为空字符串 ("")。input is an empty string ("").

- 或 --or- input 不包含日期和时间的有效字符串表示形式。input does not contain a valid string representation of a date and time.

- 或 --or- format 是一个空字符串。format is an empty string.

- 或 --or- input 中的小时组件和 AM/PM 指示符不一致。The hour component and the AM/PM designator in input do not agree.

示例

下面的示例使用带有标准和自定义格式说明符的 DateTimeOffset.ParseExact(String, String, IFormatProvider) 方法以及用于分析多个日期和时间字符串的固定区域性。The following example uses the DateTimeOffset.ParseExact(String, String, IFormatProvider) method with standard and custom format specifiers and the invariant culture to parse several date and time strings.

string dateString, format;  
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture.
dateString = "06/15/2008";
format = "d";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}   
catch (FormatException)
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
} 

// Parse date-only value without leading zero in month using "d" format.
// Should throw a FormatException because standard short date pattern of 
// invariant culture requires two-digit month.
dateString = "6/15/2008";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
} 

// Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
} 

// Parse date and time with offset without offset//s minutes.
// Should throw a FormatException because "zzz" specifier requires leading  
// zero in hours.
dateString = "Sun 15 Jun 2008 8:30 AM -06";
try
{
   result = DateTimeOffset.ParseExact(dateString, format, provider);
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
   Console.WriteLine("{0} is not in the correct format.", dateString);
} 
// The example displays the following output:
//    06/15/2008 converts to 6/15/2008 12:00:00 AM -07:00.
//    6/15/2008 is not in the correct format.
//    Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 8:30:00 AM -06:00.
//    Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.                     
Dim dateString, format As String  
Dim result As DateTimeOffset
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture.
dateString = "06/15/2008"
format = "d"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider)
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("{0} is not in the correct format.", dateString)
End Try 

' Parse date-only value without leading zero in month using "d" format.
' Should throw a FormatException because standard short date pattern of 
' invariant culture requires two-digit month.
dateString = "6/15/2008"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider)
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("{0} is not in the correct format.", dateString)
End Try 

' Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
format = "ddd dd MMM yyyy h:mm tt zzz"        
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider)
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("{0} is not in the correct format.", dateString)
End Try 

' Parse date and time with offset without offset's minutes.
' Should throw a FormatException because "zzz" specifier requires leading  
' zero in hours.
dateString = "Sun 15 Jun 2008 8:30 AM -06"
Try
   result = DateTimeOffset.ParseExact(dateString, format, provider)
   Console.WriteLine("{0} converts to {1}.", dateString, result.ToString())
Catch e As FormatException
   Console.WriteLine("{0} is not in the correct format.", dateString)
End Try 
' The example displays the following output:
'    06/15/2008 converts to 6/15/2008 12:00:00 AM -07:00.
'    6/15/2008 is not in the correct format.
'    Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 8:30:00 AM -06:00.
'    Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.                     

下面的示例分析应符合ISO 8601的字符串数组。The following example parses an array of strings that are expected to conform to ISO 8601. 如示例中的输出所示,具有前导空格或尾随空格的字符串未能成功分析,这是指具有超出范围的日期和时间元素的字符串。As the output from the example shows, strings with leading or trailing spaces fail to parse successfully, as do strings with date and time elements that are out of range.

using System;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "2018-08-18T12:45:16.0000000Z",
                               "2018/08/18T12:45:16.0000000Z",
                               "2018-18-08T12:45:16.0000000Z",
                               " 2018-08-18T12:45:16.0000000Z ",
                               "2018-08-18T12:45:16.0000000+02:00",
                               "2018-08-18T12:45:16.0000000-07:00" }; 
      
      foreach (var dateString in dateStrings)
      {
         try {
            var date = DateTimeOffset.ParseExact(dateString, "O", null);
            Console.WriteLine($"{dateString,-35} --> {date:yyyy-MM-dd HH:mm:ss.FF zzz}");
         }
         catch (FormatException)
         {
            Console.WriteLine($"FormatException: Unable to convert '{dateString}'");
         }   
      } 
   }
}
// The example displays the following output:
//      2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//      FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
//      FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
//      FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//      2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//      2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
Public Module Example
   Public Sub Main()
      Dim dateStrings() As String = { "2018-08-18T12:45:16.0000000Z",
                                      "2018/08/18T12:45:16.0000000Z",
                                      "2018-18-08T12:45:16.0000000Z",
                                      " 2018-08-18T12:45:16.0000000Z ",
                                      "2018-08-18T12:45:16.0000000+02:00",
                                      "2018-08-18T12:45:16.0000000-07:00" } 
      
      For Each dateStr In dateStrings
         Try 
            Dim dat = DateTimeOffset.ParseExact(dateStr, "O", Nothing)
            Console.WriteLine($"{dateStr,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         Catch  e As FormatException
            Console.WriteLine($"FormatException: Unable to convert '{dateStr}'")
         End Try   
      Next 
   End Sub
End Module
' The example displays the following output:
'      2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'      FormatException: Unable to convert '2018/08/18T12:45:16.0000000Z'
'      FormatException: Unable to convert '2018-18-08T12:45:16.0000000Z'
'      FormatException: Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'      2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'      2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

注解

ParseExact(String, String, IFormatProvider) 方法分析日期的字符串表示形式,该日期必须采用 format 参数定义的格式。The ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter. 它还要求日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移 > 元素按 format指定的顺序出现。It also requires that the <Date>, <Time>, and <Offset> elements of the string representation of a date and time appear in the order specified by format. 如果 input 字符串与此 format 参数不匹配,则该方法将引发一个 FormatExceptionIf the input string does not match this format parameter, the method throws a FormatException. 与此相反,DateTimeOffset.Parse(String, IFormatProvider) 方法会分析格式提供程序的 DateTimeFormatInfo 对象识别的任何格式的日期的字符串表示形式。In contrast, the DateTimeOffset.Parse(String, IFormatProvider) method parses the string representation of a date in any one of the formats recognized by the format provider's DateTimeFormatInfo object. Parse 还允许以任意顺序显示日期和时间的字符串表示形式的 <日期 >、<时间 > 和 <偏移量。Parse also allows the <Date>, <Time>, and <Offset> elements of the string representation of a date and time to appear in any order.

format 参数是包含单个标准格式说明符的字符串,或是一个或多个定义 input 参数所需格式的自定义格式说明符。The format parameter is a string that contains either a single standard format specifier or one or more custom format specifiers that define the required format of the input parameter. 有关有效格式设置代码的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. 如果 format 包括 zzzzzz 自定义格式说明符,以指示在 input中必须存在偏移量,则该偏移必须包含负号或正号。If format includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. 如果缺少符号,则方法会引发 FormatExceptionIf the sign is missing, the method throws a FormatException.

如果 format 要求 input 包含日期但不包含时间,则生成的 DateTimeOffset 对象的时间为午夜(0:00:00)。If format requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). 如果 format 要求 input 包含时间而不包含日期,则生成的 DateTimeOffset 对象将被分配到本地系统上的当前日期。If format requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. 如果 format 不要求 input 包含偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。If format does not require that input contain an offset, the resulting DateTimeOffset object is assigned the time zone offset of the local system.

input 中使用的特定日期和时间符号和字符串由 formatProvider 参数定义,如果 format 是标准格式说明符字符串,则它是 input 的精确格式。The particular date and time symbols and strings used in input are defined by the formatProvider parameter, as is the precise format of input if format is a standard format specifier string. formatProvider 参数可以是以下项之一:The formatProvider parameter can be either of the following:

如果 null``formatprovider,则使用与当前区域性对应的 CultureInfo 对象。If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

调用方说明

.NET Framework 4.NET Framework 4中,如果要分析的字符串包含一个小时部分和一个不在协议中的 AM/PM 指示符,则 ParseExact 方法将引发一个 FormatExceptionIn the .NET Framework 4.NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. .NET Framework 3.5.NET Framework 3.5 及更早版本中,将忽略 AM/PM 指示符。In the .NET Framework 3.5.NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

另请参阅

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)

使用指定的格式、区域性特定的格式信息和样式将包含日期和时间的字符串表示形式的字符范围转换为其等效的 DateTimeOffsetConverts a character span that contains the string representation of a date and time to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. 日期和时间表示形式的格式必须与一种指定的格式完全匹配。The format of the date and time representation must match one of the specified formats exactly.

public static DateTimeOffset ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, Optional styles As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTimeOffset

参数

input
ReadOnlySpan<Char>

包含要转换的日期和时间的字符范围。A character span that contains a date and time to convert.

formats
String[]

一个由格式说明符组成的数组,格式说明符用于定义 input 的期望格式。An array of format specifiers that define the expected formats of input.

formatProvider
IFormatProvider

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

styles
DateTimeStyles

枚举值的一个按位组合,指示 input 所允许的格式。A bitwise combination of enumeration values that indicates the permitted format of input.

返回

一个对象,它等效于 input 参数中包含的日期和时间,由 formatsformatProviderstyles 参数指定。An object that is equivalent to the date and time that is contained in the input parameter, as specified by the formats, formatProvider, and styles parameters.

例外

偏移量大于 14 小时或小于 -14 小时。The offset is greater than 14 hours or less than -14 hours.
-或- styles 包含不支持的值。-or- styles includes an unsupported value.
- 或 --or-
styles 参数包含不能一起使用的 DateTimeStyles 值。The styles parameter contains DateTimeStyles values that cannot be used together.

input 是一个空的字符范围。input is an empty character span.
-或- input 不包含日期和时间的有效字符串表示形式。-or- input does not contain a valid string representation of a date and time.
- 或 --or-
formats 的元素均不包含有效的格式说明符。No element of formats contains a valid format specifier.
- 或 --or-
input 中的小时组件和 AM/PM 指示符不一致。The hour component and the AM/PM designator in input do not agree.

注解

此方法分析表示日期的字符跨度,该日期与分配给 formats 参数的模式之一匹配。This method parses a character span representing a date that matches any one of the patterns assigned to the formats parameter. 如果 input styles 与上述任意一种模式都不匹配,则该方法将引发一个 FormatExceptionIf input does not match any one of these patterns with any of the variations defined by the styles parameter, the method throws a FormatException. 除了将 input 与多个格式设置模式进行比较以外,此重载的行为与 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 方法相同。Aside from comparing input to multiple formatting patterns, this overload behaves identically to the DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) method.

formats 参数是一个字符串数组,其元素包含单个标准格式说明符,或者包含一个或多个定义 input 参数的可能模式的自定义格式说明符。The formats parameter is a string array whose elements contain either a single standard format specifier or one or more custom format specifiers that define the possible pattern of the input parameter. 调用方法时,input 必须匹配其中一种模式。When the method is called, input must match one of these patterns. 有关有效格式设置代码的详细信息,请参阅标准日期和时间格式字符串自定义日期和时间格式字符串For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. 如果 formats 中的匹配的元素包括 zzzzzz 自定义格式说明符,以指示在 input中必须存在偏移量,则该偏移量必须包括负号或正负号。If the matched element in formats includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. 如果缺少符号,则方法会引发 FormatExceptionIf the sign is missing, the method throws a FormatException.

重要

使用此重载的 formats 参数指定多种格式可帮助减少许多用户在输入日期和时间时遇到的挫折。Using the formats parameter of this overload to specify multiple formats can help reduce the frustration many users experience when they enter dates and times. 特别是,定义多个输入模式的功能使应用程序能够处理日期和时间表示形式,该表示形式可以包括或缺少前导零(以月、天、小时、分钟和秒为单位)。In particular, the ability to define multiple input patterns enables an application to handle date and time representations that can either include or lack leading zeros in months, days, hours, minutes, and seconds.

如果 formats 中的匹配元素要求 input 包含日期但不包含时间,则生成的 DateTimeOffset 对象的时间为午夜(0:00:00)。If the matched element in formats requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). 如果 formats 中的匹配元素要求 input 包含时间而不包含日期,则生成的 DateTimeOffset 对象将被分配到本地系统上的当前日期。If the matched element in formats requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. 如果 formats 中的匹配元素不要求 input 包含偏移量,则生成的 DateTimeOffset 对象的偏移量取决于 styles 参数的值。If the matched element in formats does not require that input contain an offset, the offset of the resulting DateTimeOffset object depends on the value of the styles parameter. 如果 styles 包括 AssumeLocal,则会将本地时区的偏移量分配给 DateTimeOffset 对象。If styles includes AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. 如果 styles 包括 AssumeUniversal,则会将协调世界时(UTC)偏移量或 + 00:00 分配给 DateTimeOffset 对象。If styles includes AssumeUniversal, the Coordinated Universal Time (UTC) offset, or +00:00, is assigned to the DateTimeOffset object. 如果这两个值均未指定,则使用本地时区的偏移量。If neither value is specified, the offset of the local time zone is used.

input 中使用的特定日期和时间符号和字符串由 formatProvider 参数定义。The particular date and time symbols and strings used in input are defined by the formatProvider parameter. 如果 formats 的匹配元素是标准格式说明符字符串,则 input的精确格式也是如此。The same is true for the precise format of input, if the matching element of formats is a standard format specifier string. formatProvider 参数可以是以下项之一:The formatProvider parameter can be either of the following:

如果 null``formatprovider,则使用与当前区域性对应的 CultureInfo 对象。If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

styles 参数定义在输入字符串中是否允许空格,指示如何分析没有显式偏移量组件的字符串,并支持在分析操作中进行 UTC 转换。The styles parameter defines whether white space is permitted in the input string, indicates how strings without an explicit offset component are parsed, and supports UTC conversion as part of the parsing operation. NoCurrentDateDefault之外,还支持 DateTimeStyles 枚举的所有成员。All members of the DateTimeStyles enumeration are supported except NoCurrentDateDefault. 下表列出了每个受支持的成员的效果。The following table lists the effect of each supported member.

DateTimeStyles 成员DateTimeStyles member 行为Behavior
AdjustToUniversal 分析 input,并在必要时将其转换为 UTC。Parses input and, if necessary, converts it to UTC. 它等效于分析字符串,然后调用返回的 DateTimeOffset 对象的 DateTimeOffset.ToUniversalTime 方法。It is equivalent to parsing a string, and then calling the DateTimeOffset.ToUniversalTime method of the returned DateTimeOffset object.
AssumeLocal 如果 formats 中的匹配元素不要求 input 包含 offset 值,则将为返回的 DateTimeOffset 对象提供本地时区的偏移量。If the matched element in formats does not require that input contain an offset value, the returned DateTimeOffset object is given the offset of the local time zone. 这是默认值。This is the default value.
AssumeUniversal 如果 formats 中的匹配元素不要求 input 包含 offset 值,则将为返回的 DateTimeOffset 对象提供 UTC 偏移量(+ 00:00)。If the matched element in formats does not require that input contain an offset value, the returned DateTimeOffset object is given the UTC offset (+00:00).
AllowInnerWhite 允许 input 包括 format未指定的内部空格。Allows input to include inner white space not specified by format. 在对字符串进行分析时,日期和时间部分之间以及单个组件中可能出现额外的空白。Extra white space can appear between date and time components and within individual components (except the offset), and is ignored when parsing the string.
AllowLeadingWhite 允许 input 包括 formats未指定的前导空格。Allows input to include leading spaces not specified by formats. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowTrailingWhite 允许 input 包括 formats未指定的尾随空格。Allows input to include trailing spaces not specified by formats. 分析字符串时,将忽略这些字符。These are ignored when parsing the string.
AllowWhiteSpaces 允许 input 包含 formats未指定的前导空格、尾随空格和内部空格。Allows input to include leading, trailing, and inner spaces not specified by formats. 分析字符串时,将忽略在 formats 中的匹配元素中未指定的所有额外空白字符。All extra white-space characters not specified in the matched element in formats are ignored when parsing the string.
None 指示 input中不允许使用额外的空白。Indicates that additional white space is not permitted in input. 空格必须完全按照 formats 的特定元素中的指定显示,才能发生匹配。White space must appear exactly as specified in a particular element of formats for a match to occur. 这是默认行为。This is the default behavior.
RoundtripKind 不起作用,因为 DateTimeOffset 结构不包括 Kind 属性。Has no effect because the DateTimeOffset structure does not include a Kind property.

适用于