DateTime.ParseExact 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将日期和时间的指定字符串表示形式转换为其 DateTime 等效形式。 字符串表示形式的格式必须与指定的格式完全匹配,否则引发异常。
重载
| 名称 | 说明 |
|---|---|
| ParseExact(String, String, IFormatProvider) |
使用指定的格式和区域性特定格式信息将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定的格式完全匹配。 |
| ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) |
使用指定的格式、区域性特定的格式信息和样式,将日期和时间的指定范围表示形式转换为等效 DateTime 的表示形式。 字符串表示形式的格式必须与指定的格式完全匹配或引发异常。 |
| ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles) |
使用指定的格式数组、区域性特定的格式信息和样式,将日期和时间的指定范围表示形式转换为等效 DateTime 的表示形式。 字符串表示形式的格式必须与指定格式中的至少一种格式完全匹配,否则引发异常。 |
| ParseExact(String, String, IFormatProvider, DateTimeStyles) |
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定的格式完全匹配或引发异常。 |
| ParseExact(String, String[], IFormatProvider, DateTimeStyles) |
使用指定的格式数组、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定格式中的至少一种格式完全匹配,否则引发异常。 |
注解
重要
日本日历中的纪元基于皇帝的统治,因此预计将改变。 例如,2019 年 5 月 1 日在 JapaneseCalendar 和 JapaneseLunisolarCalendar 中标志着令和年号的开始。 这种时代变化会影响使用这些日历的所有应用程序。 有关详细信息以及确定您的应用程序是否受到影响,请参阅 在 .NET 中处理日语日历的新纪元。 有关在 Windows 系统上测试应用程序以确保其为纪元更改做好准备的信息,请参阅 为日本时代更改准备应用程序。 有关支持具有多个纪元的日历以及处理支持多个纪元的日历的最佳做法的 .NET 中的功能,请参阅 “使用纪元”。
ParseExact(String, String, IFormatProvider)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式和区域性特定格式信息将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定的格式完全匹配。
public:
static DateTime ParseExact(System::String ^ s, System::String ^ format, IFormatProvider ^ provider);
public static DateTime ParseExact(string s, string format, IFormatProvider provider);
public static DateTime ParseExact(string s, string format, IFormatProvider? provider);
static member ParseExact : string * string * IFormatProvider -> DateTime
Public Shared Function ParseExact (s As String, format As String, provider As IFormatProvider) As DateTime
参数
- s
- String
包含要转换的日期和时间的字符串。
- format
- String
定义所需格式的格式 s说明符。 有关详细信息,请参阅“备注”部分。
- provider
- IFormatProvider
一个对象,提供有关特定于区域性的格式信息 s。
返回
一个对象,等效于所s指定的formatprovider日期和时间。
例外
s 或 format 为 null.
示例
以下示例演示了该方法 ParseExact 。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
// Parse date-only value with invariant culture.
dateString = "06/15/2008";
format = "d";
try {
result = DateTime.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 = DateTime.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 = DateTime.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 but 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 = DateTime.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);
}
dateString = "15/06/2008 08:30";
format = "g";
provider = new CultureInfo("fr-FR");
try {
result = DateTime.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 a date that includes seconds and milliseconds
// by using the French (France) and invariant cultures.
dateString = "18/08/2015 06:30:15.006542";
format = "dd/MM/yyyy HH:mm:ss.ffffff";
try {
result = DateTime.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.
// 6/15/2008 is not in the correct format.
// Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
// Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
// 15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
// 18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let provider = CultureInfo.InvariantCulture
// Parse date-only value with invariant culture.
let dateString = "06/15/2008"
let format = "d"
try
let result = DateTime.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 = DateTime.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 = DateTime.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 but 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 = DateTime.ParseExact(dateString, format, provider)
printfn $"{dateString} converts to {result}."
with :? FormatException ->
printfn $"{dateString} is not in the correct format."
let dateString = "15/06/2008 08:30"
let format = "g"
let provider = CultureInfo "fr-FR"
try
let result = DateTime.ParseExact(dateString, format, provider)
printfn $"{dateString} converts to {result}."
with :? FormatException ->
printfn $"{dateString} is not in the correct format."
// Parse a date that includes seconds and milliseconds
// by using the French (France) and invariant cultures.
let dateString = "18/08/2015 06:30:15.006542"
let format = "dd/MM/yyyy HH:mm:ss.ffffff"
try
let result = DateTime.ParseExact(dateString, format, provider)
printfn $"{dateString} converts to {result}."
with :? FormatException ->
printfn $"{dateString} is not in the correct format."
0
// The example displays the following output:
// 06/15/2008 converts to 6/15/2008 12:00:00 AM.
// 6/15/2008 is not in the correct format.
// Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
// Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
// 15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
// 18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
Imports System.Globalization
Module Example
Public Sub Main()
Dim dateString, format As String
Dim result As Date
Dim provider As CultureInfo = CultureInfo.InvariantCulture
' Parse date-only value with invariant culture.
dateString = "06/15/2008"
format = "d"
Try
result = Date.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 = Date.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 = Date.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 but 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 = Date.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 a date string using the French (France) culture.
dateString = "15/06/2008 08:30"
format = "g"
provider = New CultureInfo("fr-FR")
Try
result = Date.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 a date that includes seconds and milliseconds
' by using the French (France) and invariant cultures.
dateString = "18/08/2015 06:30:15.006542"
format = "dd/MM/yyyy HH:mm:ss.ffffff"
Try
result = Date.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
End Sub
End Module
' The example displays the following output:
' 06/15/2008 converts to 6/15/2008 12:00:00 AM.
' 6/15/2008 is not in the correct format.
' Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
' Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
' 15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
' 18/08/2015 06:30:15.006542 converts to 8/18/2015 6:30:15 AM.
注解
该方法 DateTime.ParseExact(String, String, IFormatProvider) 分析日期的字符串表示形式,该表示形式必须采用参数定义的 format 格式。 它还要求<日期和时间的字符串表示形式的日期和时间<>>元素按指定的format顺序显示,并且s没有允许format的空格。 如果 format 定义没有时间元素的日期,并且分析操作成功,则生成的 DateTime 值为午夜(00:00:00)。 如果 format 定义没有日期元素且分析操作成功的时间,则生成的 DateTime 值为日期 DateTime.Now.Date。
如果未 s 表示特定时区中的时间,并且分析操作成功, Kind 则返回 DateTime 的值的属性为 DateTimeKind.Unspecified。 如果 s 确实表示特定时区中的时间并允许 format 存在时区信息(例如,如果 format 等于“o”、“r”或“u”标准格式说明符,或者如果它包含“z”、“zz”或“zzz”自定义格式说明符), Kind 则 DateTime 返回的值的属性是 DateTimeKind.Local。
参数 format 是一个字符串,其中包含单个标准格式说明符,或一个或多个定义所需格式的 s自定义格式说明符。 有关有效格式代码的详细信息,请参阅 标准日期和时间格式字符串 或 自定义日期和时间格式字符串。
注释
如果 format 自定义格式模式不包含日期或时间分隔符(如“yyyyMMddHmm”),则使用 provider 参数的固定区域性和每个自定义格式说明符的最宽格式。 例如,如果要以格式模式指定小时数,请指定更宽的窗体“HH”,而不是较窄的窗体“H”。
使用的特定日期和时间符号和字符串(如特定语言中s星期几的名称)由provider参数定义,就像是format标准格式说明符字符串的精确格式s一样。 该 provider 参数可以是以下任一参数:
一个 CultureInfo 对象,表示用于解释
s的区域性。 DateTimeFormatInfo其DateTimeFormat属性返回的对象定义符号s和格式。定义 DateTimeFormatInfo 日期和时间数据格式的对象。
一个自定义 IFormatProvider 实现,该方法 GetFormat 返回 CultureInfo 提供格式信息的对象或 DateTimeFormatInfo 对象。
null如果是provider,CultureInfo则使用对应于当前区域性的对象。
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则 ParseExact 该方法将引发一 FormatException 个。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- 分析 .NET Framework 中的日期和时间字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
适用于
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式、区域性特定的格式信息和样式,将日期和时间的指定范围表示形式转换为等效 DateTime 的表示形式。 字符串表示形式的格式必须与指定的格式完全匹配或引发异常。
public static DateTime ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
public static DateTime ParseExact(ReadOnlySpan<char> s, ReadOnlySpan<char> format, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), provider As IFormatProvider, Optional style As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTime
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的日期和时间的字符。
- format
- ReadOnlySpan<Char>
一个范围,包含表示定义所需格式的格式说明符的 s字符。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- style
- DateTimeStyles
枚举值的按位组合,提供有关、可能存在的s样式元素或从s值转换DateTime的其他信息s。 要指定 None的典型值为 。
返回
一个对象,它等效于由
适用于
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式数组、区域性特定的格式信息和样式,将日期和时间的指定范围表示形式转换为等效 DateTime 的表示形式。 字符串表示形式的格式必须与指定格式中的至少一种格式完全匹配,否则引发异常。
public static DateTime ParseExact(ReadOnlySpan<char> s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
public static DateTime ParseExact(ReadOnlySpan<char> s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style = System.Globalization.DateTimeStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As ReadOnlySpan(Of Char), formats As String(), provider As IFormatProvider, Optional style As DateTimeStyles = System.Globalization.DateTimeStyles.None) As DateTime
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的日期和时间的字符。
- formats
- String[]
允许格式的 s数组。
- provider
- IFormatProvider
一个对象,提供有关特定于区域性的格式信息 s。
- style
- DateTimeStyles
指示允许格式的 s枚举值的按位组合。 要指定 None的典型值为 。
返回
一个对象,它等效于由
适用于
ParseExact(String, String, IFormatProvider, DateTimeStyles)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定的格式完全匹配或引发异常。
public:
static DateTime ParseExact(System::String ^ s, System::String ^ format, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style);
public static DateTime ParseExact(string s, string format, IFormatProvider provider, System.Globalization.DateTimeStyles style);
public static DateTime ParseExact(string s, string format, IFormatProvider? provider, System.Globalization.DateTimeStyles style);
static member ParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As String, format As String, provider As IFormatProvider, style As DateTimeStyles) As DateTime
参数
- s
- String
包含要转换的日期和时间的字符串。
- format
- String
定义所需格式的格式 s说明符。 有关详细信息,请参阅“备注”部分。
- provider
- IFormatProvider
一个对象,提供有关区域性特定的格式设置信息 s。
- style
- DateTimeStyles
枚举值的按位组合,提供有关、可能存在的s样式元素或从s值转换DateTime的其他信息s。 要指定 None的典型值为 。
返回
一个对象,它等效于由
例外
s 或 format 为 null.
style 包含值的无效组合 DateTimeStyles 。 例如,AssumeLocal 和 AssumeUniversal。
示例
以下示例演示了该方法 ParseExact(String, String, IFormatProvider) 。 请注意,当参数相等DateTimeStyles.None时,无法成功分析字符串“5/01/2009 8:30 AM”,styles因为前导空格不允许。format 此外,字符串“5/01/2009 09:00”无法成功解析为 format “MM/dd/yyyyhh:mm”,因为日期字符串在月份编号之前没有前导零,因此 format 需要。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo enUS = new CultureInfo("en-US");
string dateString;
DateTime dateValue;
// Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM";
try {
dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Use custom formats with M and MM.
dateString = "5/01/2009 09:00";
try {
dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00";
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Allow a leading space in the date string.
try {
dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS,
DateTimeStyles.AdjustToUniversal);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
// Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z";
try {
dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
try {
dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind);
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue,
dateValue.Kind);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
}
}
}
// The example displays the following output:
// ' 5/01/2009 8:30 AM' is not in an acceptable format.
// Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
// Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
// '5/01/2009 09:00' is not in an acceptable format.
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
open System
open System.Globalization
[<EntryPoint>]
let main _ =
let enUS = CultureInfo "en-US"
// Parse date with no style flags.
let dateString = " 5/01/2009 8:30 AM"
try
let dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
try
let dateValue = DateTime.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Use custom formats with M and MM.
let dateString = "5/01/2009 09:00"
try
let dateValue = DateTime.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
try
let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Parse a string with time zone information.
let dateString = "05/01/2009 01:30:42 PM -05:00"
try
let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Allow a leading space in the date string.
try
let dateValue = DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.AdjustToUniversal)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// Parse a string representing UTC.
let dateString = "2008-06-11T16:11:20.0904778Z"
try
let dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
try
let dateValue = DateTime.ParseExact(dateString, "o", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
printfn $"Converted '{dateString}' to {dateValue} ({dateValue.Kind})."
with :? FormatException ->
printfn $"'{dateString}' is not in an acceptable format."
// The example displays the following output:
// ' 5/01/2009 8:30 AM' is not in an acceptable format.
// Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
// Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
// '5/01/2009 09:00' is not in an acceptable format.
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
// Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
// Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
Imports System.Globalization
Module Example
Public Sub Main()
Dim enUS As New CultureInfo("en-US")
Dim dateString As String
Dim dateValue As Date
' Parse date with no style flags.
dateString = " 5/01/2009 8:30 AM"
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "g", enUS, DateTimeStyles.AllowLeadingWhite)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Use custom formats with M and MM.
dateString = "5/01/2009 09:00"
Try
dateValue = Date.ParseExact(dateString, "M/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Parse a string with time zone information.
dateString = "05/01/2009 01:30:42 PM -05:00"
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Allow a leading space in the date string.
Try
dateValue = Date.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, _
DateTimeStyles.AdjustToUniversal)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
' Parse a string representing UTC.
dateString = "2008-06-11T16:11:20.0904778Z"
Try
dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
Try
dateValue = Date.ParseExact(dateString, "o", CultureInfo.InvariantCulture, _
DateTimeStyles.RoundtripKind)
Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, _
dateValue.Kind)
Catch e As FormatException
Console.WriteLine("'{0}' is not in an acceptable format.", dateString)
End Try
End Sub
End Module
' The example displays the following output:
' ' 5/01/2009 8:30 AM' is not in an acceptable format.
' Converted ' 5/01/2009 8:30 AM' to 5/1/2009 8:30:00 AM (Unspecified).
' Converted '5/01/2009 09:00' to 5/1/2009 9:00:00 AM (Unspecified).
' '5/01/2009 09:00' is not in an acceptable format.
' Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 11:30:42 AM (Local).
' Converted '05/01/2009 01:30:42 PM -05:00' to 5/1/2009 6:30:42 PM (Utc).
' Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 9:11:20 AM (Local).
' Converted '2008-06-11T16:11:20.0904778Z' to 6/11/2008 4:11:20 PM (Utc).
注解
该方法 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 分析日期的字符串表示形式,该表示形式必须采用 format 参数定义的格式。 它还要求日期和时间元素 s 按指定的 format顺序显示。 如果 s 参数模式 format 与参数定义 style 的任何变体不匹配,该方法将引发一个 FormatException。 相反,该方法 DateTime.Parse(String, IFormatProvider, DateTimeStyles) 以格式提供程序对象识别的任何格式分析日期的 DateTimeFormatInfo 字符串表示形式。 该方法 DateTime.Parse(String, IFormatProvider, DateTimeStyles) 还允许日期和时间元素 s 按任意顺序显示。
s如果参数仅包含时间和日期,则使用当前日期,除非style参数包含DateTimeStyles.NoCurrentDateDefault标志,在这种情况下使用默认日期(DateTime.Date.MinValue)。
s如果参数仅包含日期且没有时间,则使用午夜 (00:00:00)。 该 style 参数还确定参数是否可以 s 包含前导、内部或尾随空格字符。
如果 s 不包含时区信息, Kind 则 DateTime 返回对象的属性为 DateTimeKind.Unspecified。 可以使用标志来更改DateTimeStyles.AssumeLocal此行为,该标志返回DateTime其Kind属性为DateTimeKind.Local的值,或者通过使用DateTimeStyles.AssumeUniversal和DateTimeStyles.AdjustToUniversal标志来更改此行为,后者返回DateTime其属性为KindDateTimeKind.Utc的值。 如果 s 包含时区信息,则时间将转换为本地时间(如有必要)并将 Kind 返回 DateTime 对象的属性设置为 DateTimeKind.Local。 可以使用标志将协调世界时(UTC)转换为本地时间并将属性DateTimeKind.Utc设置为 来更改DateTimeStyles.RoundtripKind此Kind行为。
该 format 参数定义参数的 s 必需模式。 它可以包含 自定义日期和时间格式字符串 表中的一个或多个自定义格式说明符,也可以包含一个标准格式说明符,该说明符标识 标准日期和时间格式字符串 表中的预定义模式。
如果不在自定义格式模式中使用日期或时间分隔符,请使用参数的固定区域性 provider 和每个自定义格式说明符的最宽格式。 例如,如果要在模式中指定小时数,请指定更宽的窗体“HH”,而不是较窄的窗体“H”。
注释
可以调用DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)该方法并指定多个允许的格式,而不是要求s符合分析操作的单个格式才能成功。 这使得分析操作更有可能成功。
该 styles 参数包括枚举的 DateTimeStyles 一个或多个成员,用于确定是否以及其中未定义 format 空格是否可显示 s ,以及控制分析操作的精确行为。 下表介绍了枚举的每个成员 DateTimeStyles 如何影响方法的操作 ParseExact(String, String, IFormatProvider, DateTimeStyles) 。
| DateTimeStyles 成员 | 说明 |
|---|---|
| AdjustToUniversal | 分析 s 并在必要时将其转换为 UTC。 如果 s 包含时区偏移量,或者 s 如果不包含时区信息,但 styles 包含 DateTimeStyles.AssumeLocal 标志,该方法将分析字符串,调用 ToUniversalTime 将返回 DateTime 的值转换为 UTC,并将属性设置为 KindDateTimeKind.Utc。 如果 s 指示它表示 UTC,或者 s 如果不包含时区信息但 styles 包含 DateTimeStyles.AssumeUniversal 标志,该方法将分析字符串,对返回 DateTime 的值不执行时区转换,并将属性设置为 KindDateTimeKind.Utc。 在所有其他情况下,标志不起作用。 |
| AllowInnerWhite | 指定未定义的 format 空格可在任何单个日期或时间元素之间显示。 |
| AllowLeadingWhite | 指定未定义的 format 空格可以出现在开头 s。 |
| AllowTrailingWhite | 指定未定义的 format 空格可以出现在结尾 s处。 |
| AllowWhiteSpaces | 指定 s 可能包含未定义的 format前导空格、内部空格和尾随空格。 |
| AssumeLocal | 指定如果 s 缺少任何时区信息,则假定它表示本地时间。 除非DateTimeStyles.AdjustToUniversal标记存在,否则返回值Kind的DateTime属性将设置为DateTimeKind.Local。 |
| AssumeUniversal | 指定如果 s 缺少任何时区信息,则假定它表示 UTC。 除非存在DateTimeStyles.AdjustToUniversal标志,否则该方法会将返回的DateTime值从 UTC 转换为本地时间,并设置其Kind属性为DateTimeKind.Local。 |
| NoCurrentDateDefault | 如果 s 包含没有日期信息的时间,则返回值的日期设置为 DateTime.MinValue.Date。 |
| None | 使用 s 默认值分析参数。 不允许除该空格以外的 format 空格。 如果 s 缺少日期组件,则 DateTime 返回值的日期设置为 1/1/0001。 如果 s 不包含时区信息, Kind 则返回 DateTime 对象的属性设置为 DateTimeKind.Unspecified。 如果存在 s时区信息,则时间将转换为本地时间,并将 Kind 返回 DateTime 对象的属性设置为 DateTimeKind.Local。 |
| RoundtripKind | 对于包含时区信息的字符串,尝试防止转换为 DateTime 值日期和时间,其 Kind 属性设置为 DateTimeKind.Local。 此标志主要阻止将 UTC 时间转换为本地时间。 |
使用的特定日期和时间符号和字符串(如特定语言中s星期几的名称)由provider参数定义,如标准format格式说明符字符串的精确格式s。 该 provider 参数可以是以下任一参数:
一个 CultureInfo 对象,表示用于解释
s的区域性。 DateTimeFormatInfo其DateTimeFormat属性返回的对象定义符号s和格式。定义 DateTimeFormatInfo 日期和时间数据格式的对象。
一个自定义 IFormatProvider 实现,该方法 GetFormat 返回 CultureInfo 提供格式信息的对象或 DateTimeFormatInfo 对象。
null如果是provider,CultureInfo则使用对应于当前区域性的对象。
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则 ParseExact 该方法将引发一 FormatException 个。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- 分析 .NET Framework 中的日期和时间字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
适用于
ParseExact(String, String[], IFormatProvider, DateTimeStyles)
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
- Source:
- DateTime.cs
使用指定的格式数组、区域性特定的格式信息和样式将日期和时间的指定字符串表示形式转换为等效 DateTime 的字符串表示形式。 字符串表示形式的格式必须与指定格式中的至少一种格式完全匹配,否则引发异常。
public:
static DateTime ParseExact(System::String ^ s, cli::array <System::String ^> ^ formats, IFormatProvider ^ provider, System::Globalization::DateTimeStyles style);
public static DateTime ParseExact(string s, string[] formats, IFormatProvider provider, System.Globalization.DateTimeStyles style);
public static DateTime ParseExact(string s, string[] formats, IFormatProvider? provider, System.Globalization.DateTimeStyles style);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles -> DateTime
Public Shared Function ParseExact (s As String, formats As String(), provider As IFormatProvider, style As DateTimeStyles) As DateTime
参数
- s
- String
包含要转换的日期和时间的字符串。
- formats
- String[]
允许格式的 s数组。 有关详细信息,请参阅“备注”部分。
- provider
- IFormatProvider
一个对象,提供有关特定于区域性的格式信息 s。
- style
- DateTimeStyles
指示允许格式的 s枚举值的按位组合。 要指定 None的典型值为 。
返回
一个对象,它等效于由
例外
s 或 formats 为 null.
s 是空字符串。
-或-
元素为 formats 空字符串。
-或-
s 不包含对应于任何元素的 formats日期和时间。
-或-
小时组件和 AM/PM 设计器 s 不同意。
style 包含值的无效组合 DateTimeStyles 。 例如,AssumeLocal 和 AssumeUniversal。
示例
下面的示例使用 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 该方法来确保可以成功分析具有多种可能格式的字符串。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt",
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss",
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt",
"M/d/yyyy h:mm", "M/d/yyyy h:mm",
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" };
string[] dateStrings = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM",
"5/1/2009 6:32:00", "05/01/2009 06:32",
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
"08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" };
DateTime dateValue;
foreach (string dateString in dateStrings)
{
try {
dateValue = DateTime.ParseExact(dateString, formats,
new CultureInfo("en-US"),
DateTimeStyles.None);
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
}
catch (FormatException) {
Console.WriteLine("Unable to convert '{0}' to a date.", dateString);
}
}
}
}
// The example displays the following output:
// Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
// Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
// Unable to convert '08/28/2015 16:17:39.125' to a date.
// Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
open System
open System.Globalization
let formats =
[| "M/d/yyyy h:mm:ss tt"; "M/d/yyyy h:mm tt"
"MM/dd/yyyy hh:mm:ss"; "M/d/yyyy h:mm:ss"
"M/d/yyyy hh:mm tt"; "M/d/yyyy hh tt"
"M/d/yyyy h:mm"; "M/d/yyyy h:mm"
"MM/dd/yyyy hh:mm"; "M/dd/yyyy hh:mm"
"MM/d/yyyy HH:mm:ss.ffffff" |]
let dateStrings =
[ "5/1/2009 6:32 PM"; "05/01/2009 6:32:05 PM"
"5/1/2009 6:32:00"; "05/01/2009 06:32"
"05/01/2009 06:32:00 PM"; "05/01/2009 06:32:00"
"08/28/2015 16:17:39.125"; "08/28/2015 16:17:39.125000" ]
for dateString in dateStrings do
try
let dateValue = DateTime.ParseExact(dateString, formats, CultureInfo "en-US", DateTimeStyles.None)
printfn $"Converted '{dateString}' to {dateValue}."
with :? FormatException ->
printfn $"Unable to convert '{dateString}' to a date."
// The example displays the following output:
// Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
// Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
// Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
// Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
// Unable to convert '08/28/2015 16:17:39.125' to a date.
// Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
Imports System.Globalization
Module Example
Public Sub Main()
Dim formats() As String = {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", _
"MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", _
"M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", _
"M/d/yyyy h:mm", "M/d/yyyy h:mm", _
"MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm",
"MM/d/yyyy HH:mm:ss.ffffff" }
Dim dateStrings() As String = {"5/1/2009 6:32 PM", "05/01/2009 6:32:05 PM", _
"5/1/2009 6:32:00", "05/01/2009 06:32", _
"05/01/2009 06:32:00 PM", "05/01/2009 06:32:00",
"08/28/2015 16:17:39.125", "08/28/2015 16:17:39.125000" }
Dim dateValue As DateTime
For Each dateString As String In dateStrings
Try
dateValue = DateTime.ParseExact(dateString, formats, _
New CultureInfo("en-US"), _
DateTimeStyles.None)
Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a date.", dateString)
End Try
Next
End Sub
End Module
' The example displays the following output:
' Converted '5/1/2009 6:32 PM' to 5/1/2009 6:32:00 PM.
' Converted '05/01/2009 6:32:05 PM' to 5/1/2009 6:32:05 PM.
' Converted '5/1/2009 6:32:00' to 5/1/2009 6:32:00 AM.
' Converted '05/01/2009 06:32' to 5/1/2009 6:32:00 AM.
' Converted '05/01/2009 06:32:00 PM' to 5/1/2009 6:32:00 PM.
' Converted '05/01/2009 06:32:00' to 5/1/2009 6:32:00 AM.
' Unable to convert '08/28/2015 16:17:39.125' to a date.
' Converted '08/28/2015 16:17:39.125000' to 8/28/2015 4:17:39 PM.
注解
该方法 DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 分析与分配给 formats 参数的任何模式匹配的日期的字符串表示形式。 如果字符串 s 与参数定义 styles 的任何变体都不匹配其中任一模式,则该方法将引发一个 FormatException。 除了与多个格式模式进行比较,而不是与单个格式模式相比 s ,此重载的行为与方法相同 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) 。
该 s 参数包含要分析的日期和时间。
s如果参数仅包含时间和日期,则使用当前日期,除非style参数包含DateTimeStyles.NoCurrentDateDefault标志,在这种情况下使用默认日期(DateTime.Date.MinValue)。
s如果参数仅包含日期且没有时间,则使用午夜 (00:00:00)。 该 style 参数还确定参数是否可以 s 包含前导、内部或尾随空格字符,而不是其中 formats一个格式字符串允许的空格字符。
如果 s 不包含时区信息, Kind 则 DateTime 返回对象的属性为 DateTimeKind.Unspecified。 可以使用标志来更改DateTimeStyles.AssumeLocal此行为,该标志返回DateTime其Kind属性为DateTimeKind.Local的值,或者通过使用DateTimeStyles.AssumeUniversal和DateTimeStyles.AdjustToUniversal标志来更改此行为,后者返回DateTime其属性为KindDateTimeKind.Utc的值。 如果 s 包含时区信息,则时间将转换为本地时间(如有必要)并将 Kind 返回 DateTime 对象的属性设置为 DateTimeKind.Local。 可以使用标志将协调世界时(UTC)转换为本地时间并将属性设置为 KindDateTimeKind.Utc 来更改DateTimeStyles.RoundtripKind此行为。
该 formats 参数包含模式数组,其中 s 一个模式必须完全匹配(如果分析操作成功)。 参数中的 formats 模式由 自定义日期和时间格式字符串 表中的一个或多个自定义格式说明符组成,或一个标准格式说明符,用于标识 标准日期和时间格式字符串 表中的预定义模式。
如果不在自定义格式模式中使用日期或时间分隔符,请使用参数的固定区域性 provider 和每个自定义格式说明符的最宽格式。 例如,如果要在模式中指定小时数,请指定更宽的窗体“HH”,而不是较窄的窗体“H”。
该 styles 参数包括枚举的 DateTimeStyles 一个或多个成员,用于确定是否以及其中未定义 format 空格是否可显示 s ,以及控制分析操作的精确行为。 下表介绍了枚举的每个成员 DateTimeStyles 如何影响方法的操作 ParseExact(String, String, IFormatProvider, DateTimeStyles) 。
| DateTimeStyles 成员 | 说明 |
|---|---|
| AdjustToUniversal | 分析 s 并在必要时将其转换为 UTC。 如果 s 包含时区偏移量,或者 s 如果不包含时区信息,但 styles 包含 DateTimeStyles.AssumeLocal 标志,该方法将分析字符串,调用 ToUniversalTime 将返回 DateTime 的值转换为 UTC,并将属性设置为 KindDateTimeKind.Utc。 如果 s 指示它表示 UTC,或者 s 如果不包含时区信息但 styles 包含 DateTimeStyles.AssumeUniversal 标志,该方法将分析字符串,对返回 DateTime 的值不执行时区转换,并将属性设置为 KindDateTimeKind.Utc。 在所有其他情况下,标志不起作用。 |
| AllowInnerWhite | 指定未定义的 format 空格可在任何单个日期或时间元素之间显示。 |
| AllowLeadingWhite | 指定未定义的 format 空格可以出现在开头 s。 |
| AllowTrailingWhite | 指定未定义的 format 空格可以出现在结尾 s处。 |
| AllowWhiteSpaces | 指定 s 可能包含未定义的 format前导空格、内部空格和尾随空格。 |
| AssumeLocal | 指定如果 s 缺少任何时区信息,则假定它表示本地时间。 除非DateTimeStyles.AdjustToUniversal标记存在,否则返回值Kind的DateTime属性将设置为DateTimeKind.Local。 |
| AssumeUniversal | 指定如果 s 缺少任何时区信息,则假定它表示 UTC。 除非存在DateTimeStyles.AdjustToUniversal标志,否则该方法会将返回的DateTime值从 UTC 转换为本地时间,并设置其Kind属性为DateTimeKind.Local。 |
| NoCurrentDateDefault | 如果 s 包含没有日期信息的时间,则返回值的日期设置为 DateTime.MinValue.Date。 |
| None | 使用 s 默认值分析参数。 不允许除该空格以外的 format 空格。 如果 s 缺少日期组件,则 DateTime 返回值的日期设置为 1/1/0001。 如果 s 不包含时区信息, Kind 则返回 DateTime 对象的属性设置为 DateTimeKind.Unspecified。 如果存在 s时区信息,则时间将转换为本地时间,并将 Kind 返回 DateTime 对象的属性设置为 DateTimeKind.Local。 |
| RoundtripKind | 对于包含时区信息的字符串,尝试阻止转换为其属性设置为DateTimeKind.Local的日期和时间Kind。 此标志主要阻止将 UTC 时间转换为本地时间。 |
使用的特定日期和时间符号和字符串(如特定语言中s星期几的名称)由provider参数定义,如标准format格式说明符字符串的精确格式s。 该 provider 参数可以是以下任一参数:
一个 CultureInfo 对象,表示用于解释
s的区域性。 DateTimeFormatInfo其DateTimeFormat属性返回的对象定义符号s和格式。定义 DateTimeFormatInfo 日期和时间数据格式的对象。
一个自定义 IFormatProvider 实现,该方法 GetFormat 返回 CultureInfo 提供格式信息的对象或 DateTimeFormatInfo 对象。
null如果是provider,CultureInfo则使用对应于当前区域性的对象。
调用方说明
在 .NET Framework 4 中,如果要分析的字符串包含一小时组件和未达成协议的 AM/PM 指示符,则 ParseExact 该方法将引发一 FormatException 个。 在 .NET Framework 3.5 及更早版本中,将忽略 AM/PM 设计器。
另请参阅
- TryParseExact
- CultureInfo
- DateTimeFormatInfo
- 分析 .NET Framework 中的日期和时间字符串
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串