DateTimeOffset.ParseExact 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与指定的格式完全匹配。
重载
| ParseExact(String, String[], IFormatProvider, DateTimeStyles) | 使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与一种指定的格式完全匹配。 | 
| ParseExact(String, String, IFormatProvider, DateTimeStyles) | 使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与指定的格式完全匹配。 | 
| ParseExact(String, String, IFormatProvider) | 使用指定的格式和区域性特定格式信息,将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与指定的格式完全匹配。 | 
| ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) | 使用指定的格式、区域性特定的格式信息和样式将表示日期和时间的字符范围转换为其等效的 DateTimeOffset。 日期和时间表示形式的格式必须与指定的格式完全匹配。 | 
| ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles) | 使用指定的格式、区域性特定的格式信息和样式将包含日期和时间的字符串表示形式的字符范围转换为其等效的 DateTimeOffset。 日期和时间表示形式的格式必须与一种指定的格式完全匹配。 | 
ParseExact(String, String[], IFormatProvider, DateTimeStyles)
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与一种指定的格式完全匹配。
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);public static DateTimeOffset ParseExact (string input, string[] formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles);static member ParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffsetPublic Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset参数
- input
- String
包含要转换的日期和时间的字符串。
- formats
- String[]
一个由格式说明符组成的数组,格式说明符用于定义 input 的期望格式。
- formatProvider
- IFormatProvider
一个对象,提供有关 input 的区域性特定格式设置信息。
- styles
- DateTimeStyles
枚举值的一个按位组合,指示 input 所允许的格式。
返回
一个对象,它等效于 input 参数中包含的日期和时间,由 formats、formatProvider 和 styles 参数指定。
例外
input 为 null。
input 为空字符串 ("")。
- 或 -
input 不包含日期和时间的有效字符串表示形式。
- 或 -
formats 的元素均不包含有效的格式说明符。
- 或 -
input 中的小时组件和 AM/PM 指示符不一致。
示例
以下示例为日期和时间和偏移值的字符串表示形式定义多个输入格式,然后将用户 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 输入的字符串传递给该方法。
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
let input = String.Empty
let formats = 
    [| @"@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" |]
let provider = CultureInfo.InvariantCulture.DateTimeFormat
let mutable result = None
let mutable tries = 0
while tries < 3 && result.IsNone do
    printfn "Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),"
    printf "Then press Enter: "
    let input = stdin.ReadLine()
    printfn ""
    try
        result <- 
            DateTimeOffset.ParseExact(input, formats, provider, DateTimeStyles.AllowWhiteSpaces) 
            |> Some
    with :? FormatException ->
        printfn $"Unable to parse {input}."
        tries <- tries + 1
match result with
| Some result ->
    printfn $"{input} was converted to {result}"
| None ->
    printfn $"Exiting application without parsing {input}"
// 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 参数的任何模式匹配的日期的字符串表示形式。 input如果字符串与参数定义styles的任何变体不匹配其中任何一种模式,则该方法将引发一个 FormatException。 除了与多个格式模式进行比较 input 外,此重载的行为与方法相同 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 。
参数 formats 是一个字符串数组,其元素包含单个标准格式说明符或定义参数可能模式的 input 一个或多个自定义格式说明符。 调用该方法时, input 必须与其中一种模式匹配。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。 如果匹配的formats元素包含z或zz``zzz自定义格式说明符来指示必须存在input偏移量,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
重要
formats使用此重载的参数指定多种格式可以帮助减少输入日期和时间时许多用户体验的挫折感。 具体而言,定义多个输入模式的功能使应用程序能够处理日期和时间表示形式,这些表示形式可以包括或缺少月份、天、小时、分钟和秒的前导零。 此示例提供了此示例的插图。
如果匹配的元素 formats 需要 input 包含日期而不是时间,则生成的 DateTimeOffset 对象将分配午夜 (0:00:00) 的时间。 如果匹配的 formats 元素需要 input 包含一个时间而不是日期,则生成的 DateTimeOffset 对象将分配本地系统上的当前日期。 如果匹配的元素 formats 不需要包含 input 偏移量,则生成的 DateTimeOffset 对象的偏移量取决于参数的值 styles 。 如果 styles 包含 AssumeLocal,则会将本地时区的偏移量分配给 DateTimeOffset 对象。 如果 styles 包含 AssumeUniversal,则为对象分配 DateTimeOffset 协调世界时 (UTC) 偏移量或 +00:00。 如果未指定两个值,则使用本地时区的偏移量。
参数定义了formatProvider所使用的input特定日期和时间符号和字符串。 如果匹配元素formats是标准格式说明符字符串,则其精确格式input也是如此。 该 formatProvider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,表示基于其 - input解释的文化。 属性DateTimeFormatInfo返回CultureInfo.DateTimeFormat的对象定义符号和格式设置。- input
- 定义 DateTimeFormatInfo 日期和时间数据格式的对象。 
null如果是formatprovider,CultureInfo则使用与当前区域性对应的对象。
该 styles 参数定义是否允许在输入字符串中使用空格,指示如何分析没有显式偏移组件的字符串,并支持在分析操作过程中进行 UTC 转换。 除  DateTimeStyles 外 NoCurrentDateDefault,支持枚举的所有成员。 下表列出了每个受支持成员的效果。
| DateTimeStyles成员 | 行为 | 
|---|---|
| AdjustToUniversal | input分析,如有必要,将其转换为 UTC。 它等效于分析字符串,然后调用 DateTimeOffset.ToUniversalTime 返回 DateTimeOffset 的对象的方法。 | 
| AssumeLocal | 如果匹配的 formats元素不需要input包含偏移值,则返回 DateTimeOffset 的对象将得到本地时区的偏移量。 这是默认值。 | 
| AssumeUniversal | 如果匹配的元素 formats不需要包含input偏移值,则返回 DateTimeOffset 的对象会获得 UTC 偏移量 (+00:00) 。 | 
| AllowInnerWhite | 允许 input包含未由format指定的内部空格。 除了偏移量 () 外,日期和时间组件之间以及各个组件之间可能会显示额外的空格,在分析字符串时将被忽略。 | 
| AllowLeadingWhite | 允许 input包含未指定formats的前导空格。 分析字符串时会忽略这些值。 | 
| AllowTrailingWhite | 允许 input包含未由formats指定的尾随空格。 分析字符串时会忽略这些值。 | 
| AllowWhiteSpaces | 允许 input包含前导、尾随和内部空格未指定formats。 分析字符串时,将忽略匹配元素formats中指定的所有额外的空格字符。 | 
| None | Indicates that additional white space is not permitted in input. 空格必须完全如特定元素formats中指定的一样显示,才能进行匹配。 此选项为默认行为。 | 
| RoundtripKind | 由于结构不包含属性 Kind,因此不起作用DateTimeOffset。 | 
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则ParseExact该方法将引发FormatException一个。 在 .NET Framework 3.5 及更低版本中,AM/PM 指示符将被忽略。
另请参阅
适用于
ParseExact(String, String, IFormatProvider, DateTimeStyles)
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与指定的格式完全匹配。
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);public static DateTimeOffset ParseExact (string input, string format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles);static member ParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffsetPublic Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset参数
- input
- String
包含要转换的日期和时间的字符串。
- format
- String
用于定义所需的 input 格式的格式说明符。
- formatProvider
- IFormatProvider
一个对象,提供有关 input 的区域性特定格式设置信息。
- styles
- DateTimeStyles
枚举值的一个按位组合,指示 input 所允许的格式。
返回
一个对象,它等效于 input 参数中包含的日期和时间,由 format、formatProvider 和 styles 参数指定。
例外
input 为空字符串 ("")。
- 或 -
input 不包含日期和时间的有效字符串表示形式。
- 或 -
format 是一个空字符串。
- 或 -
input 中的小时组件和 AM/PM 指示符不一致。
示例
以下示例使用 DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) 具有标准和自定义格式说明符、固定区域性和各种 DateTimeStyles 值的方法来分析多个日期和时间字符串。
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.
let provider = CultureInfo.InvariantCulture
// Parse date-only value with invariant culture and assume time is UTC.
let dateString = "06/15/2008"
let format = "d"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider, DateTimeStyles.AssumeUniversal)
    printfn $"'{dateString}' converts to {result}."
with :? FormatException ->
    printfn $"'{dateString}' is not in the correct format."
// Parse date-only value with leading white space.
// Should throw a FormatException because only trailing white space is
// specified in method call.
let dateString = " 06/15/2008"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider, DateTimeStyles.AllowTrailingWhite)
    printfn $"'{dateString}' converts to {result}."
with :? FormatException ->
    printfn $"'{dateString}' is not in the correct format."
// Parse date and time value, and allow all white space.
let dateString = " 06/15/   2008  15:15    -05:00"
let format = "MM/dd/yyyy H:mm zzz"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces)
    printfn $"'{dateString}' converts to {result}."
with :? FormatException ->
    printfn $"'{dateString}' is not in the correct format."
// Parse date and time and convert to UTC.
let dateString = "  06/15/2008 15:15:30 -05:00"
let format = "MM/dd/yyyy H:mm:ss zzz"
try
    let result = 
        DateTimeOffset.ParseExact(dateString, format, provider,
                                  DateTimeStyles.AllowWhiteSpaces |||
                                  DateTimeStyles.AdjustToUniversal)
    printfn $"'{dateString}' converts to {result}."
with :? FormatException ->
    printfn $"'{dateString}' is not in the correct format."
// 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 的字符串数组。 如示例中的输出所示,如果以下情况,格式正确的字符串将无法分析:
- 它们包含空格,并且方法调用中未提供相应的 DateTimeStyles 标志 (,例如 DateTimeStyles.AllowWhiteSpaces 尚未提供。 
- 它们包含范围不足的日期和时间元素。 
假定未指定 UTC 偏移量的字符串在本例中为 -07:00) 本地时区 (偏移量,除非 DateTimeStyles.AssumeUniversal 方法调用中提供了标志。 在这种情况下,它们假定为世界协调时间。
module parseexact_iso8601_2
open System
open System.Globalization
let 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" |] 
let parseWithISO8601 dateStrings styles =
    printfn $"Parsing with {styles}:"
    for dateString in dateStrings do
        try
            let date = DateTimeOffset.ParseExact(dateString, "O", null, styles)
            printfn $"""   {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
        with :? FormatException ->
            printfn $"   FormatException: Unable to convert '{dateString}'"
parseWithISO8601 dateStrings DateTimeStyles.None
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal
// 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
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 参数定义的格式。 它还要求<Date><Time>日期和时间的字符串表示形式和<Offset>元素按指定的format顺序显示。 input如果字符串与参数的format模式不匹配,且参数定义styles的任何变体,该方法将引发一个 FormatException。  相反,该方法 DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) 以格式提供程序对象识别的任何格式分析日期的 DateTimeFormatInfo 字符串表示形式。 Parse还允许<Date><Time>日期和时间的字符串表示形式的元素<Offset>按任意顺序显示。
该 format 参数是一个字符串,其中包含单个标准格式说明符或一个或多个定义参数所需模式的 input 自定义格式说明符。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。 如果format包含z或zz``zzz自定义格式说明符以指示偏移必须存在于input其中,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
如果 format 要求包含 input 日期而不是某个时间,则会将生成的 DateTimeOffset 对象指定为午夜 (0:00:00) 。 如果需要 format 包含 input 一个时间而不是日期,则生成的 DateTimeOffset 对象将分配本地系统上的当前日期。 如果 format 不需要包含 input 偏移量,则生成的 DateTimeOffset 对象的偏移量取决于参数的值 styles 。 如果 styles 包含 AssumeLocal,则会将本地时区的偏移量 DateTimeOffset 分配给对象。 如果 styles 包含 AssumeUniversal,则向对象分配 DateTimeOffset 协调世界时 (UTC) 偏移量或 +00:00。 如果未指定任何值,则使用本地时区的偏移量。
参数定义了formatProvider用于input的特定日期和时间符号和字符串。 对于精确格式 input(如果 format 为标准格式说明符字符串)也是如此。 该 formatProvider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,表示基于其 - input解释的区域性。 DateTimeFormatInfo属性返回CultureInfo.DateTimeFormat的对象定义符号和格式设置。- input
- 定义 DateTimeFormatInfo 日期和时间数据格式的对象。 
null如果是formatprovider,CultureInfo则使用对应于当前区域性的对象。
该 styles 参数定义是否允许在输入字符串中使用空格,指示如何分析没有显式偏移量的字符串,并支持 UTC 转换作为分析操作的一部分。 除  DateTimeStyles 外 NoCurrentDateDefault,支持枚举的所有成员。 下表列出了每个受支持成员的效果。
| DateTimeStyles成员 | 行为 | 
|---|---|
| AdjustToUniversal | 分析 input并在必要时将其转换为 UTC。 它等效于分析字符串,然后调用 DateTimeOffset.ToUniversalTime 返回 DateTimeOffset 的对象的方法。 | 
| AssumeLocal | 如果 format不需要包含input偏移值,则返回 DateTimeOffset 的对象将得到本地时区的偏移量。 这是默认值。 | 
| AssumeUniversal | 如果 format不需要包含input偏移值,则 DateTimeOffset 返回的对象将给定 UTC 偏移量 (+00:00) 。 | 
| AllowInnerWhite | 允许 input包含未指定format的内部空格。 在日期和时间组件之间以及各个组件之间可以显示额外的空白,在分析字符串时将被忽略。 | 
| AllowLeadingWhite | 允许 input包含未由format. 指定的前导空格。 分析字符串时会忽略这些值。 | 
| AllowTrailingWhite | 允许 input包含未指定format尾随空格。 分析字符串时会忽略这些值。 | 
| AllowWhiteSpaces | 允许 input包含前导、尾随和未指定format内部空格。 分析字符串时,不会忽略未指定的所有format额外空格字符。 | 
| None | 指示不允许使用 input其他空格。 空格必须完全如在中format指定一样显示。 此选项为默认行为。 | 
| RoundtripKind | 由于结构不包含 Kind属性,因此不起作用DateTimeOffset。 | 
调用方说明
在 .NET Framework 4 中,该方法ParseExact会引发一个FormatException如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 设计器。 在 .NET Framework 3.5 及更早版本中,AM/PM 设计器将被忽略。
另请参阅
适用于
ParseExact(String, String, IFormatProvider)
使用指定的格式和区域性特定格式信息,将日期和时间的指定字符串表示形式转换为其等效的 DateTimeOffset。 字符串表示形式的格式必须与指定的格式完全匹配。
public:
 static DateTimeOffset ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);public static DateTimeOffset ParseExact (string input, string format, IFormatProvider formatProvider);public static DateTimeOffset ParseExact (string input, string format, IFormatProvider? formatProvider);static member ParseExact : string * string * IFormatProvider -> DateTimeOffsetPublic Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As DateTimeOffset参数
- input
- String
包含要转换的日期和时间的字符串。
- format
- String
用于定义所需的 input 格式的格式说明符。
- formatProvider
- IFormatProvider
一个对象,提供有关 input 的区域性特定格式设置信息。
返回
一个对象,它等效于 input 中包含的日期和时间,由 format 和 formatProvider 指定。
例外
偏移量大于 14 小时或小于 -14 小时。
input 为空字符串 ("")。
- 或 -
input 不包含日期和时间的有效字符串表示形式。
- 或 -
format 是一个空字符串。
- 或 -
input 中的小时组件和 AM/PM 指示符不一致。
示例
以下示例使用 DateTimeOffset.ParseExact(String, String, IFormatProvider) 具有标准和自定义格式说明符的方法以及固定区域性来分析多个日期和时间字符串。
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.
let provider = CultureInfo.InvariantCulture
// Parse date-only value with invariant culture.
let dateString = "06/15/2008"
let format = "d"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider)
    printfn $"{dateString} converts to {result}."
with :? FormatException ->
    printfn $"{dateString} is not in the correct format."
// 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.
let dateString = "6/15/2008"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider)
    printfn $"{dateString} converts to {result}."
with :? FormatException ->
    printfn $"{dateString} is not in the correct format."
// Parse date and time with custom specifier.
let dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
let format = "ddd dd MMM yyyy h:mm tt zzz"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider)
    printfn $"{dateString} converts to {result}."
with :? FormatException ->
    printfn $"{dateString} is not in the correct format."
// Parse date and time with offset without offset//s minutes.
// Should throw a FormatException because "zzz" specifier requires leading
// zero in hours.
let dateString = "Sun 15 Jun 2008 8:30 AM -06"
try
    let result = DateTimeOffset.ParseExact(dateString, format, provider)
    printfn $"{dateString} converts to {result}."
with :? FormatException ->
    printfn $"{dateString} is not in the correct format."
// 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 的字符串数组。 如示例中的输出所示,具有前导或尾随空格的字符串无法成功分析,而具有日期和时间元素的字符串也不符合范围。
module parseexact_iso8601
open System
let 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" ]
for dateString in dateStrings do
    try
        let date =
            DateTimeOffset.ParseExact(dateString, "O", null)
        printfn $"""{dateString, -35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
    with :? FormatException -> printfn $"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
using System;
public class Example2
{
   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 参数定义的格式。 它还要求<Date><Time>日期和时间的字符串表示形式和<Offset>元素按指定的format顺序显示。 input如果字符串与此参数format不匹配,该方法将引发一个 FormatException。  相反,该方法 DateTimeOffset.Parse(String, IFormatProvider) 以格式提供程序对象识别的任何格式分析日期的 DateTimeFormatInfo 字符串表示形式。 Parse还允许<Date><Time><Offset>日期和时间的字符串表示形式的元素按任意顺序显示。
参数 format 是一个字符串,其中包含单个标准格式说明符或一个或多个定义参数所需格式的 input 自定义格式说明符。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。 如果format包含z或zz``zzz自定义格式说明符来指示必须存在input偏移量,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
如果需要 format 包含 input 日期而不是某个时间,则会将生成的 DateTimeOffset 对象分配为午夜 (0:00:00) 。 如果需要 format 包含 input 一个时间而不是日期,则生成的 DateTimeOffset 对象将分配本地系统上的当前日期。 如果 format 不需要包含 input 偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。
所使用的input特定日期和时间符号和字符串由formatProvider参数定义,就像是标准格式说明符字符串的精确格式input``format一样。 该 formatProvider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,表示基于其 - input解释的文化。 DateTimeFormatInfo由其DateTimeFormat属性返回的对象定义符号和格式设置。- input
- 定义 DateTimeFormatInfo 日期和时间数据格式的对象。 
null如果是formatprovider,CultureInfo则使用与当前区域性对应的对象。
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则ParseExact该方法将引发FormatException一个。 在 .NET Framework 3.5 及更低版本中,AM/PM 指示符将被忽略。
另请参阅
适用于
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)
使用指定的格式、区域性特定的格式信息和样式将表示日期和时间的字符范围转换为其等效的 DateTimeOffset。 日期和时间表示形式的格式必须与指定的格式完全匹配。
public static DateTimeOffset ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);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 -> DateTimeOffsetPublic 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>
一个表示日期和时间的字符范围。
- format
- ReadOnlySpan<Char>
包含格式说明符的字符范围,用于定义 input 的预期格式。
- formatProvider
- IFormatProvider
一个对象,提供有关 input 的区域性特定格式设置信息。
- styles
- DateTimeStyles
枚举值的一个按位组合,指示 input 所允许的格式。
返回
一个对象,它等效于 input 参数中包含的日期和时间,由 format、formatProvider 和 styles 参数指定。
例外
偏移量大于 14 小时或小于 -14 小时。
- 或 - 参数 styles包含不受支持的值。
- 或 - 参数 styles包含 DateTimeStyles 不能一起使用的值。
input 是一个空的字符范围。
-或- input 不包含日期和时间的有效字符串表示形式。
-或- format 是一个空的字符范围。
- 或 - 小时组件和 AM/PM 指示符 input不同意。
注解
此方法分析表示日期的字符范围,该范围必须采用 format 参数定义的格式。 它还要求<Date><Time>日期和时间的字符串表示形式和<Offset>元素按指定的format顺序显示。 如果 input 与模式不匹配 format ,该方法将引发一个 FormatException。  相反,该方法 DateTimeOffset.Parse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 以格式提供程序对象识别的任何格式分析日期的 DateTimeFormatInfo 字符串表示形式。 Parse还允许<Date><Time><Offset>日期和时间的字符串表示形式的元素按任意顺序显示。
参数 format 是一个字符范围,其中包含单字符标准格式说明符或定义参数所需格式的 input 一个或多个自定义格式说明符。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。 如果format包含z或zz``zzz自定义格式说明符来指示必须存在input偏移量,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
如果需要 format 包含 input 日期而不是某个时间,则会将生成的 DateTimeOffset 对象分配为午夜 (0:00:00) 。 如果需要 format 包含 input 一个时间而不是日期,则生成的 DateTimeOffset 对象将分配本地系统上的当前日期。 如果 format 不需要包含 input 偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。
参数定义了formatProvider所使用的input特定日期和时间符号和字符串,就像标准格式说明符的精确格式input``format一样。 该 formatProvider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,表示基于其 - input解释的文化。 DateTimeFormatInfo由其DateTimeFormat属性返回的对象定义符号和格式设置。- input
- 定义 DateTimeFormatInfo 日期和时间数据格式的对象。 
null如果是formatprovider,CultureInfo则使用与当前区域性对应的对象。
适用于
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)
使用指定的格式、区域性特定的格式信息和样式将包含日期和时间的字符串表示形式的字符范围转换为其等效的 DateTimeOffset。 日期和时间表示形式的格式必须与一种指定的格式完全匹配。
public static DateTimeOffset ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles = System.Globalization.DateTimeStyles.None);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 -> DateTimeOffsetPublic 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>
包含要转换的日期和时间的字符范围。
- formats
- String[]
一个由格式说明符组成的数组,格式说明符用于定义 input 的期望格式。
- formatProvider
- IFormatProvider
一个对象,提供有关 input 的区域性特定格式设置信息。
- styles
- DateTimeStyles
枚举值的一个按位组合,指示 input 所允许的格式。
返回
一个对象,它等效于 input 参数中包含的日期和时间,由 formats、formatProvider 和 styles 参数指定。
例外
偏移量大于 14 小时或小于 -14 小时。
-或- styles 包含不支持的值。
- 或 - 参数 styles包含 DateTimeStyles 不能一起使用的值。
input 是一个空的字符范围。
-或- input 不包含日期和时间的有效字符串表示形式。
- 或 - 不包含有效格式说明符的 formats元素。
- 或 - 小时组件和 AM/PM 设计器 input不同意。
注解
此方法分析表示与分配给参数的任何模式匹配的日期的 formats 字符范围。 如果 input 与参数定义 styles 的任何变体不匹配其中任何一种模式,该方法将引发一个 FormatException。 除了与多个格式模式进行比较 input 外,此重载的行为与方法相同 DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) 。
参数 formats 是一个字符串数组,其元素包含单个标准格式说明符或定义参数可能模式的 input 一个或多个自定义格式说明符。 调用该方法时, input 必须与其中一种模式匹配。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。 如果匹配的formats元素包含z或zz``zzz自定义格式说明符来指示必须存在input偏移量,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
重要
formats使用此重载的参数指定多种格式可以帮助减少输入日期和时间时许多用户体验的挫折感。 具体而言,定义多个输入模式的功能使应用程序能够处理日期和时间表示形式,这些表示形式可以包括或缺少月份、天、小时、分钟和秒的前导零。
如果匹配的元素 formats 需要 input 包含日期而不是时间,则生成的 DateTimeOffset 对象将分配午夜 (0:00:00) 的时间。 如果匹配的 formats 元素需要 input 包含一个时间而不是日期,则生成的 DateTimeOffset 对象将分配本地系统上的当前日期。 如果匹配的元素 formats 不需要包含 input 偏移量,则生成的 DateTimeOffset 对象的偏移量取决于参数的值 styles 。 如果 styles 包含 AssumeLocal,则会将本地时区的偏移量分配给 DateTimeOffset 对象。 如果 styles 包含 AssumeUniversal,则为对象分配 DateTimeOffset 协调世界时 (UTC) 偏移量或 +00:00。 如果未指定两个值,则使用本地时区的偏移量。
参数定义了formatProvider所使用的input特定日期和时间符号和字符串。 如果匹配元素formats是标准格式说明符字符串,则其精确格式input也是如此。 该 formatProvider 参数可以是以下任一参数:
- 一个 CultureInfo 对象,表示基于其 - input解释的文化。 属性DateTimeFormatInfo返回CultureInfo.DateTimeFormat的对象定义符号和格式设置。- input
- 定义 DateTimeFormatInfo 日期和时间数据格式的对象。 
null如果是formatprovider,CultureInfo则使用与当前区域性对应的对象。
该 styles 参数定义是否允许在输入字符串中使用空格,指示如何分析没有显式偏移组件的字符串,并支持在分析操作过程中进行 UTC 转换。 除  DateTimeStyles 外 NoCurrentDateDefault,支持枚举的所有成员。 下表列出了每个受支持成员的效果。
| DateTimeStyles成员 | 行为 | 
|---|---|
| AdjustToUniversal | 分析 input并在必要时将其转换为 UTC。 它等效于分析字符串,然后调用 DateTimeOffset.ToUniversalTime 返回 DateTimeOffset 的对象的方法。 | 
| AssumeLocal | 如果匹配的元素 formats不需要包含input偏移值,则返回 DateTimeOffset 的对象将得到本地时区的偏移量。 这是默认值。 | 
| AssumeUniversal | 如果匹配的元素 formats不需要包含input偏移值,则返回 DateTimeOffset 的对象将给定 UTC 偏移量 (+00:00) 。 | 
| AllowInnerWhite | 允许 input包含未指定format的内部空格。 除偏移量 () 外,日期和时间组件之间以及各个组件之间可能会显示额外的空格,并在分析字符串时被忽略。 | 
| AllowLeadingWhite | 允许 input包含未由formats. 指定的前导空格。 分析字符串时会忽略这些值。 | 
| AllowTrailingWhite | 允许 input包含未指定formats尾随空格。 分析字符串时会忽略这些值。 | 
| AllowWhiteSpaces | 允许 input包含前导、尾随和未指定formats内部空格。 分析字符串时,将忽略匹配元素formats中未指定的所有额外的空格字符。 | 
| None | 指示不允许使用 input其他空格。 空格必须与特定元素formats中的指定完全一样显示,才能进行匹配。 此选项为默认行为。 | 
| RoundtripKind | 由于结构不包含 Kind属性,因此不起作用DateTimeOffset。 |