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)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 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 -> DateTimeOffset
Public 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日期和时间等效的对象,由formatsformatProvider参数和styles参数指定。
例外
input 是 null。
input 是空字符串(“)。
-或-
input 不包含日期和时间的有效字符串表示形式。
-或-
不包含有效格式说明符的 formats 元素。
-或-
小时组件和 AM/PM 设计器 input 不同意。
示例
以下示例为日期和时间和偏移值的字符串表示形式定义多个输入格式,然后将用户输入的字符串传递给 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 | 指示不允许在 . 中 input增加空格。 空格必须完全如特定元素 formats 中指定的一样显示,才能进行匹配。 这是默认行为。 |
| RoundtripKind | 由于结构不包含Kind属性,因此不起作用DateTimeOffset。 |
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则 ParseExact 该方法将引发一 FormatException 个。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
适用于
ParseExact(String, String, IFormatProvider, DateTimeStyles)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 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 -> DateTimeOffset
Public 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日期和时间等效的对象,由formatformatProvider参数和styles参数指定。
例外
input 是空字符串(“)。
-或-
input 不包含日期和时间的有效字符串表示形式。
-或-
format 是空字符串。
-或-
小时组件和 AM/PM 设计器 input 不同意。
示例
以下示例使用 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或zzzzz自定义格式说明符以指示偏移必须存在于input其中,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
input如果需要format包含日期而不是时间,则会为生成的DateTimeOffset对象分配午夜 (0:00:00: 00)。
input如果需要format包含一个时间而不是日期,则会在本地系统上为生成的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 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则 ParseExact 该方法将引发一 FormatException 个。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
适用于
ParseExact(String, String, IFormatProvider)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的格式和区域性特定格式信息将日期和时间的指定字符串表示形式转换为等效 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 -> DateTimeOffset
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As DateTimeOffset
参数
- input
- String
包含要转换的日期和时间的字符串。
- format
- String
定义预期格式的格式 input说明符。
- formatProvider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 input。
返回
一个对象,它等效于由和formatProvider指定的format日期和时间中包含的input日期和时间。
例外
偏移量大于 14 小时或小于 -14 小时。
input 是空字符串(“)。
-或-
input 不包含日期和时间的有效字符串表示形式。
-或-
format 是空字符串。
-或-
小时组件和 AM/PM 设计器 input 不同意。
示例
以下示例使用 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或zzzzz自定义格式说明符以指示偏移必须存在于input其中,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
input如果需要format包含日期而不是时间,则会为生成的DateTimeOffset对象分配午夜 (0:00:00: 00)。
input如果需要format包含一个时间而不是日期,则会在本地系统上为生成的DateTimeOffset对象分配当前日期。 如果 format 不需要包含 input 偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。
参数定义了formatProvider用于input的特定日期和时间符号和字符串,就像是format标准格式说明符字符串的精确格式input一样。 该 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)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的格式、区域性特定的格式信息和样式将表示日期和时间的字符范围转换为等效 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 -> 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>
表示日期和时间的字符范围。
- format
- ReadOnlySpan<Char>
一个字符范围,包含定义预期格式的格式 input说明符。
- formatProvider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 input。
- styles
- DateTimeStyles
指示允许格式的 input枚举值的按位组合。
返回
与参数中包含的input日期和时间等效的对象,由formatformatProvider参数和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或zzzzz自定义格式说明符以指示偏移必须存在于input其中,则偏移必须包含负号或正号。 如果缺少符号,该方法将引发一个 FormatException。
input如果需要format包含日期而不是时间,则会为生成的DateTimeOffset对象分配午夜 (0:00:00: 00)。
input如果需要format包含一个时间而不是日期,则会在本地系统上为生成的DateTimeOffset对象分配当前日期。 如果 format 不需要包含 input 偏移量,则会为生成的 DateTimeOffset 对象分配本地系统的时区偏移量。
参数定义了formatProvider用于input的特定日期和时间符号和字符串,就像标准format格式说明符的精确格式input一样。 该 formatProvider 参数可以是下列任一参数:
一个 CultureInfo 对象,表示基于其
input解释的区域性。 DateTimeFormatInfo其DateTimeFormat属性返回的对象定义符号input和格式。定义 DateTimeFormatInfo 日期和时间数据格式的对象。
null如果是formatprovider,CultureInfo则使用对应于当前区域性的对象。
适用于
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
- Source:
- DateTimeOffset.cs
使用指定的格式、区域性特定的格式信息和样式将包含日期和时间的字符串表示形式的字符范围转换为等效 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 -> 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>
包含要转换的日期和时间的字符范围。
- formats
- String[]
用于定义预期格式 input的格式说明符数组。
- formatProvider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 input。
- styles
- DateTimeStyles
指示允许格式的 input枚举值的按位组合。
返回
与参数中包含的input日期和时间等效的对象,由formatsformatProvider参数和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。 |