TimeSpan.ParseExact 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将时间间隔的字符串表示形式转换为等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
重载
ParseExact(String, String, IFormatProvider, TimeSpanStyles) |
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。 |
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles) |
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。 |
ParseExact(String, String[], IFormatProvider, TimeSpanStyles) |
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。 |
ParseExact(String, String[], IFormatProvider) |
使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。 |
ParseExact(String, String, IFormatProvider) |
使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。 |
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) |
使用指定的格式和区域性特定格式信息,将时间间隔的字符型范围转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。 |
ParseExact(String, String, IFormatProvider, TimeSpanStyles)
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
public:
static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan
参数
- input
- String
一个字符串,用于指定进行转换的时间间隔。
- format
- String
用于定义所需的 input
格式的标准或自定义格式字符串。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
- styles
- TimeSpanStyles
枚举值的按位组合,用于定义可出现在 input
中的样式元素。
返回
与 input
对应的时间间隔,由 format
、formatProvider
和 styles
指定。
例外
styles
不是有效的 TimeSpanStyles 值。
input
为 null
。
input
的格式无效。
示例
下面的示例使用 ParseExact(String, String, IFormatProvider) 方法来分析使用各种格式字符串和区域性的时间间隔的几个字符串表示形式。 它还使用 TimeSpanStyles.AssumeNegative 值将每个字符串解释为一个负时间间隔。 该示例的输出说明了 TimeSpanStyles.AssumeNegative 样式仅在与自定义格式字符串一起使用时才影响返回值。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture = null;
// Parse hour:minute value with custom format specifier.
intervalString = "17:14";
format = "h\\:mm";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hour:minute:second value with "g" specifier.
intervalString = "17:14:48";
format = "g";
culture = CultureInfo.InvariantCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hours:minute.second value with custom format specifier.
intervalString = "17:14:48.153";
format = @"h\:mm\:ss\.fff";
culture = null;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with a custom format specifier.
intervalString = "3:17:14:48.153";
format = @"d\:hh\:mm\:ss\.fff";
culture = null;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
culture = new CultureInfo("fr-FR");
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%h" custom format string.
format = "%h";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%s" custom format string.
format = "%s";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
}
}
// The example displays the following output:
// '17:14' (h\:mm) --> -17:14:00
// '17:14:48' (g) --> 17:14:48
// '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
// '3:17:14:48.153' (G) --> 3.17:14:48.1530000
// '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
// '3:17:14:48,153' (G) --> 3.17:14:48.1530000
// '12' (c) --> 12.00:00:00
// '12' (%h) --> -12:00:00
// '12' (%s) --> -00:00:12
Imports System.Globalization
Module Example
Public Sub Main()
Dim intervalString, format As String
Dim interval As TimeSpan
Dim culture As CultureInfo = Nothing
' Parse hour:minute value with custom format specifier.
intervalString = "17:14"
format = "h\:mm"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hour:minute:second value with "g" specifier.
intervalString = "17:14:48"
format = "g"
culture = CultureInfo.InvariantCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hours:minute.second value with custom format specifier.
intervalString = "17:14:48.153"
format = "h\:mm\:ss\.fff"
culture = Nothing
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with a custom format specifier.
intervalString = "3:17:14:48.153"
format = "d\:hh\:mm\:ss\.fff"
culture = Nothing
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48,153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "c" standard format string.
intervalString = "12"
format = "c"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%h" custom format string.
format = "%h"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%s" custom format string.
format = "%s"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
End Sub
End Module
' The example displays the following output:
' '17:14' (h\:mm) --> -17:14:00
' '17:14:48' (g) --> 17:14:48
' '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
' '3:17:14:48.153' (G) --> 3.17:14:48.1530000
' '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
' '3:17:14:48,153' (G) --> 3.17:14:48.1530000
' '12' (c) --> 12.00:00:00
' '12' (%h) --> -12:00:00
' '12' (%s) --> -00:00:12
注解
ParseExact方法分析时间间隔的字符串表示形式,该时间间隔必须采用参数定义的格式 format
,但会忽略前导和尾随空白字符。 由于 input
必须严格符合格式,因此 format
,在将用户的字符串输入转换为时间间隔时,应始终使用异常处理。 如果不想使用异常处理,可以改为调用 TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) 方法。
format
参数是一个字符串,其中包含单个标准格式说明符,或者一个或多个定义所需的格式的自定义格式说明符 input
。 有关有效的格式字符串的详细信息,请参阅 标准 Timespan 格式字符串 和 自定义的 timespan 格式字符串。
重要
ParseExact formatProvider
仅当 format
是标准 TimeSpan 格式字符串(其值为 "G" 或 "g")时,方法才使用由参数指定的区域性的约定。 "C"、"t" 和 "T" 标准格式字符串使用固定区域性的格式设置约定。 自定义格式字符串定义输入字符串的精确格式,并使用原义字符分隔时间间隔的组成部分。
formatProvider
参数是一个 IFormatProvider 实现,提供有关返回字符串格式的区域性特定信息,前提 format
是标准格式字符串。 formatProvider
参数可以是以下任一项:
一个 CultureInfo 对象,该对象表示要在返回的字符串中反映其格式设置约定的区域性。 DateTimeFormatInfo由属性返回的对象 CultureInfo.DateTimeFormat 定义返回的字符串的格式设置。
一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式设置。
一个实现接口的自定义对象 IFormatProvider 。 其 IFormatProvider.GetFormat 方法返回 DateTimeFormatInfo 提供格式设置信息的对象。
如果 formatProvider
为 null
,则 DateTimeFormatInfo 使用与当前区域性关联的对象。
styles
参数影响使用自定义格式字符串分析的字符串的解释。 它确定 input
是仅当存在负号时才会解释为负时间间隔 (TimeSpanStyles.None) ,或者是否始终将其解释为 () 的负时间间隔 TimeSpanStyles.AssumeNegative 。 如果 TimeSpanStyles.AssumeNegative 未使用,则 format
必须包含文本负号符号 (如 " \ -" ) 才能成功分析负时间间隔。
另请参阅
适用于
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。
public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan
参数
- input
- ReadOnlySpan<Char>
一个范围,用于指定要转换的时间间隔。
- formats
- String[]
用于定义所需的 input
格式的标准或自定义格式字符串的数组。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
- styles
- TimeSpanStyles
枚举值的按位组合,用于定义可出现在 input 中的样式元素。
返回
与 input
对应的时间间隔,由 formats
、formatProvider
和 styles
指定。
适用于
ParseExact(String, String[], IFormatProvider, TimeSpanStyles)
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。
public:
static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan
参数
- input
- String
一个字符串,用于指定进行转换的时间间隔。
- formats
- String[]
用于定义所需的 input
格式的标准或自定义格式字符串的数组。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
- styles
- TimeSpanStyles
枚举值的按位组合,用于定义可出现在 input 中的样式元素。
返回
与 input
对应的时间间隔,由 formats
、formatProvider
和 styles
指定。
例外
styles
不是有效的 TimeSpanStyles 值。
input
为 null
。
input
的格式无效。
示例
下面的示例调用 ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 方法,将字符串数组的每个元素转换为一个 TimeSpan 值。 这些字符串可以表示常规短格式或常规长格式的时间间隔。
此外,该示例还更改了时间间隔分析方法解释单个数字的方式。 通常,单个数字被解释为时间间隔中的天数。 而是 %h
使用自定义格式字符串将一个数字解释为小时数。 要使此更改生效,请注意, %h
自定义格式字符串必须位于数组中的其他格式字符串之前 formats
。 另请注意, TimeSpanStyles.AssumeNegative 只有在使用此格式说明符分析字符串的情况下,才会使用在方法调用中指定的标志。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "%h", "g", "G" };
TimeSpan interval;
CultureInfo culture = new CultureInfo("de-DE");
// Parse each string in inputs using formats and the de-DE culture.
foreach (string input in inputs) {
try {
interval = TimeSpan.ParseExact(input, formats, culture,
TimeSpanStyles.AssumeNegative);
Console.WriteLine("{0} --> {1:c}", input, interval);
}
catch (FormatException) {
Console.WriteLine("{0} --> Bad Format", input);
}
catch (OverflowException) {
Console.WriteLine("{0} --> Overflow", input);
}
}
}
}
// The example displays the following output:
// 3 --> -03:00:00
// 16:42 --> 16:42:00
// 1:6:52:35.0625 --> Bad Format
// 1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization
Module Example
Public Sub Main()
Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" }
Dim formats() As String = { "%h", "g", "G" }
Dim interval As TimeSpan
Dim culture As New CultureInfo("de-DE")
' Parse each string in inputs using formats and the de-DE culture.
For Each input As String In inputs
Try
interval = TimeSpan.ParseExact(input, formats, culture,
TimeSpanStyles.AssumeNegative)
Console.WriteLine("{0} --> {1:c}", input, interval)
Catch e As FormatException
Console.WriteLine("{0} --> Bad Format", input)
Catch e As OverflowException
Console.WriteLine("{0} --> Overflow", input)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 3 --> -03:00:00
' 16:42 --> 16:42:00
' 1:6:52:35.0625 --> Bad Format
' 1:6:52:35,0625 --> 1.06:52:35.0625000
注解
ParseExact(String, String[], IFormatProvider, TimeSpanStyles)方法分析时间间隔的字符串表示形式,该时间间隔必须采用参数定义的格式之一 formats
,但会忽略前导和尾随空白字符。 由于 input
必须与中指定的格式之一完全一致,因此,在将 formats
用户的字符串输入转换为时间间隔时,应始终使用异常处理。 如果不想使用异常处理,可以改为调用 TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) 方法。
formats
参数是一个字符串数组,其元素由一个标准格式说明符或一个或多个定义所需格式的自定义格式说明符组成 input
。 有关有效的格式字符串的详细信息,请参阅 标准 Timespan 格式字符串 和 自定义的 timespan 格式字符串。 input
必须完全与成员相对应,以便 formats
分析操作成功。 分析操作尝试与数组中 input
第一个元素开头的每个元素匹配 formats
。
重要
ParseExact formatProvider
仅当用于分析的格式字符串 input
是标准 TimeSpan 格式字符串(其值为 "G" 或 "g")时,方法才使用由参数指定的区域性约定。 "C"、"t" 和 "T" 标准格式字符串使用固定区域性的格式设置约定。 自定义格式字符串定义输入字符串的精确格式,并使用原义字符分隔时间间隔的组成部分。
formatProvider
IFormatProvider 如果用于分析的格式字符串 input
是标准格式字符串,则该参数是一个实现,提供有关返回字符串格式的区域性特定信息。 formatProvider
参数可以是以下任一项:
一个 CultureInfo 对象,该对象表示要在返回的字符串中反映其格式设置约定的区域性。 DateTimeFormatInfo由属性返回的对象 CultureInfo.DateTimeFormat 定义返回的字符串的格式设置。
一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式设置。
一个实现接口的自定义对象 IFormatProvider 。 其 IFormatProvider.GetFormat 方法返回 DateTimeFormatInfo 提供格式设置信息的对象。
如果 formatProvider
为 null
,则 DateTimeFormatInfo 使用与当前区域性关联的对象。
styles
参数影响使用自定义格式字符串分析的字符串的解释。 它确定 input
是仅当存在负号时才会解释为负时间间隔 (TimeSpanStyles.None) ,或者是否始终将其解释为 () 的负时间间隔 TimeSpanStyles.AssumeNegative 。 如果 TimeSpanStyles.AssumeNegative 未使用,则 format
必须包含文本负号符号 (如 " \ -" ) 才能成功分析负时间间隔。
另请参阅
适用于
ParseExact(String, String[], IFormatProvider)
使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。
public:
static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider);
static member ParseExact : string * string[] * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider) As TimeSpan
参数
- input
- String
一个字符串,用于指定进行转换的时间间隔。
- formats
- String[]
用于定义所需的 input
格式的标准或自定义格式字符串的数组。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
返回
与 input
对应的时间间隔,由 formats
和 formatProvider
指定。
例外
input
为 null
。
input
的格式无效。
示例
下面的示例调用 ParseExact(String, String[], IFormatProvider) 方法,将字符串数组的每个元素转换为一个 TimeSpan 值。 该示例使用法语-法国 ( "fr" ) 区域性的格式设置约定来解释字符串。 这些字符串可以表示常规短格式或常规长格式的时间间隔。
此外,该示例还更改了时间间隔分析方法解释单个数字的方式。 通常,单个数字被解释为时间间隔中的天数。 而是 %h
使用自定义格式字符串将一个数字解释为小时数。 要使此更改生效,请注意, %h
自定义格式字符串必须位于数组中的其他格式字符串之前 formats
。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "g", "G", "%h"};
TimeSpan interval;
CultureInfo culture = new CultureInfo("fr-FR");
// Parse each string in inputs using formats and the fr-FR culture.
foreach (string input in inputs) {
try {
interval = TimeSpan.ParseExact(input, formats, culture);
Console.WriteLine("{0} --> {1:c}", input, interval);
}
catch (FormatException) {
Console.WriteLine("{0} --> Bad Format", input);
}
catch (OverflowException) {
Console.WriteLine("{0} --> Overflow", input);
}
}
}
}
// The example displays the following output:
// 3 --> 03:00:00
// 16:42 --> 16:42:00
// 1:6:52:35.0625 --> Bad Format
// 1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization
Module Example
Public Sub Main()
Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" }
Dim formats() As String = { "%h", "g", "G" }
Dim interval As TimeSpan
Dim culture As New CultureInfo("fr-FR")
' Parse each string in inputs using formats and the fr-FR culture.
For Each input As String In inputs
Try
interval = TimeSpan.ParseExact(input, formats, culture)
Console.WriteLine("{0} --> {1:c}", input, interval)
Catch e As FormatException
Console.WriteLine("{0} --> Bad Format", input)
Catch e As OverflowException
Console.WriteLine("{0} --> Overflow", input)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 3 --> 3.00:00:00
' 16:42 --> 16:42:00
' 1:6:52:35.0625 --> Bad Format
' 1:6:52:35,0625 --> 1.06:52:35.0625000
注解
ParseExact(String, String, IFormatProvider)方法分析时间间隔的字符串表示形式,该时间间隔必须采用参数定义的格式之一 formats
,但会忽略前导和尾随空白字符。 由于 input
必须与中指定的格式之一完全一致,因此,在将 formats
用户的字符串输入转换为时间间隔时,应始终使用异常处理。 如果不想使用异常处理,可以改为调用 TryParseExact(String, String[], IFormatProvider, TimeSpan) 方法。
formats
参数是一个字符串数组,其元素由一个标准格式说明符或一个或多个定义所需格式的自定义格式说明符组成 input
。 有关有效的格式字符串的详细信息,请参阅 标准 Timespan 格式字符串 和 自定义的 timespan 格式字符串。 input
必须完全与成员相对应,以便 formats
分析操作成功。 分析操作尝试与数组中 input
第一个元素开头的每个元素匹配 formats
。
重要
ParseExact formatProvider
仅当用于分析的格式字符串 input
是标准 TimeSpan 格式字符串(其值为 "G" 或 "g")时,方法才使用由参数指定的区域性约定。 "C"、"t" 和 "T" 标准格式字符串使用固定区域性的格式设置约定。 自定义格式字符串定义输入字符串的精确格式,并使用原义字符分隔时间间隔的组成部分。
formatProvider
IFormatProvider 如果用于分析的格式字符串 input
是标准格式字符串,则该参数是一个实现,提供有关返回字符串格式的区域性特定信息。 formatProvider
参数可以是以下任一项:
一个 CultureInfo 对象,该对象表示要在返回的字符串中反映其格式设置约定的区域性。 DateTimeFormatInfo由属性返回的对象 CultureInfo.DateTimeFormat 定义返回的字符串的格式设置。
一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式设置。
一个实现接口的自定义对象 IFormatProvider 。 其 IFormatProvider.GetFormat 方法返回 DateTimeFormatInfo 提供格式设置信息的对象。
如果 formatProvider
为 null
,则 DateTimeFormatInfo 使用与当前区域性关联的对象。
另请参阅
适用于
ParseExact(String, String, IFormatProvider)
使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
public:
static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider);
static member ParseExact : string * string * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As TimeSpan
参数
- input
- String
一个字符串,用于指定进行转换的时间间隔。
- format
- String
用于定义所需的 input
格式的标准或自定义格式字符串。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
返回
与 input
对应的时间间隔,由 format
和 formatProvider
指定。
例外
input
为 null
。
input
的格式无效。
示例
下面的示例使用 ParseExact(String, String, IFormatProvider) 方法来分析使用各种格式字符串和区域性的时间间隔的几个字符串表示形式。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture;
// Parse hour:minute value with "g" specifier current culture.
intervalString = "17:14";
format = "g";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'",
intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hour:minute:second value with "G" specifier.
intervalString = "17:14:48";
format = "G";
culture = CultureInfo.InvariantCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = new CultureInfo("fr-FR");
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%h" custom format string.
format = "%h";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%s" custom format string.
format = "%s";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
}
}
// The example displays the following output:
// '17:14' --> 17:14:00
// '17:14:48': Bad Format for 'G'
// '17:14:48.153': Bad Format for 'G'
// '3:17:14:48.153' --> 3.17:14:48.1530000
// '3:17:14:48.153': Bad Format for 'G'
// '3:17:14:48,153' --> 3.17:14:48.1530000
// '12' --> 12.00:00:00
// '12' --> 12:00:00
// '12' --> 00:00:12
Imports System.Globalization
Module Example
Public Sub Main()
Dim intervalString, format As String
Dim interval As TimeSpan
Dim culture As CultureInfo
' Parse hour:minute value with "g" specifier current culture.
intervalString = "17:14"
format = "g"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hour:minute:second value with "G" specifier.
intervalString = "17:14:48"
format = "G"
culture = CultureInfo.InvariantCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48,153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "c" standard format string.
intervalString = "12"
format = "c"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%h" custom format string.
format = "%h"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%s" custom format string.
format = "%s"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
End Sub
End Module
' The example displays the following output:
' '17:14' --> 17:14:00
' '17:14:48': Bad Format for 'G'
' '17:14:48.153': Bad Format for 'G'
' '3:17:14:48.153' --> 3.17:14:48.1530000
' '3:17:14:48.153': Bad Format for 'G'
' '3:17:14:48,153' --> 3.17:14:48.1530000
' '12' --> 12.00:00:00
' '12' --> 12:00:00
' '12' --> 00:00:12
注解
ParseExact(String, String, IFormatProvider)方法分析时间间隔的字符串表示形式,该时间间隔必须采用参数定义的格式 format
,但会忽略前导和尾随空白字符。 由于 input
必须严格符合格式,因此 format
,在将用户的字符串输入转换为时间间隔时,应始终使用异常处理。 如果不想使用异常处理,可以改为调用 TryParseExact(String, String, IFormatProvider, TimeSpan) 方法。
format
参数是一个字符串,其中包含单个标准格式说明符,或者一个或多个定义所需的格式的自定义格式说明符 input
。 有关有效的格式字符串的详细信息,请参阅 标准 Timespan 格式字符串 和 自定义的 timespan 格式字符串。
重要
ParseExact formatProvider
仅当 format
是标准 TimeSpan 格式字符串(其值为 "G" 或 "g")时,方法才使用由参数指定的区域性的约定。 "C"、"t" 和 "T" 标准格式字符串使用固定区域性的格式设置约定。 自定义格式字符串定义输入字符串的精确格式,并使用原义字符分隔时间间隔的组成部分。
formatProvider
参数是一个 IFormatProvider 实现,提供有关返回字符串格式的区域性特定信息,前提 format
是标准格式字符串。 formatProvider
参数可以是以下任一项:
一个 CultureInfo 对象,该对象表示要在返回的字符串中反映其格式设置约定的区域性。 DateTimeFormatInfo由属性返回的对象 CultureInfo.DateTimeFormat 定义返回的字符串的格式设置。
一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式设置。
一个实现接口的自定义对象 IFormatProvider 。 其 IFormatProvider.GetFormat 方法返回 DateTimeFormatInfo 提供格式设置信息的对象。
如果 formatProvider
为 null
,则 DateTimeFormatInfo 使用与当前区域性关联的对象。
另请参阅
适用于
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)
使用指定的格式和区域性特定格式信息,将时间间隔的字符型范围转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan
参数
- input
- ReadOnlySpan<Char>
一个范围,用于指定要转换的时间间隔。
- format
- ReadOnlySpan<Char>
用于定义所需的 input
格式的标准或自定义格式字符串。
- formatProvider
- IFormatProvider
一个对象,提供区域性特定的格式设置信息。
- styles
- TimeSpanStyles
枚举值的按位组合,用于定义可出现在 input
中的样式元素。
返回
与 input
对应的时间间隔,由 format
和 formatProvider
指定。