TimeSpan.ParseExact 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将时间间隔的字符串表示形式转换为等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。
重载
ParseExact(String, String[], IFormatProvider, TimeSpanStyles) |
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与一种指定的格式完全匹配。 |
ParseExact(String, String, IFormatProvider, TimeSpanStyles) |
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpan。 字符串表示形式的格式必须与指定的格式完全匹配。 |
ParseExact(ReadOnlySpan<Char>, 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)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 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
open System
open System.Globalization
let inputs =
[| "3"; "16:42"; "1:6:52:35.0625"; "1:6:52:35,0625" |]
let formats = [| "%h"; "g"; "G" |]
let culture = CultureInfo "de-DE"
// Parse each string in inputs using formats and the de-DE culture.
for input in inputs do
try
let interval =
TimeSpan.ParseExact(input, formats, culture, TimeSpanStyles.AssumeNegative)
printfn $"{input} --> {interval:c}"
with
| :? FormatException ->
printfn $"{input} --> Bad Format"
| :? OverflowException ->
printfn $"{input} --> Overflow"
// 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
从第一个元素开始的每个元素。
重要
仅当用于分析input
的格式字符串是值为“g”或“G”的标准TimeSpan格式字符串时,方法ParseExact才使用 参数指定的formatProvider
区域性的约定。 “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, TimeSpanStyles)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 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
open System
open System.Globalization
do
// Parse hour:minute value with custom format specifier.
let intervalString = "17:14"
let format = "h\\:mm"
let culture = CultureInfo.CurrentCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse hour:minute:second value with "g" specifier.
let intervalString = "17:14:48"
let format = "g"
let culture = CultureInfo.InvariantCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse hours:minute.second value with custom format specifier.
let intervalString = "17:14:48.153"
let format = @"h\:mm\:ss\.fff"
let culture = null
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
let intervalString = "3:17:14:48.153"
let format = "G"
let culture = CultureInfo.CurrentCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with a custom format specifier.
let intervalString = "3:17:14:48.153"
let format = @"d\:hh\:mm\:ss\.fff"
let culture = null
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
let intervalString = "3:17:14:48,153"
let format = "G"
let culture = new CultureInfo("fr-FR")
try
let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "c" standard format string.
let intervalString = "12"
let format = "c"
try
let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "%h" custom format string.
let format = "%h"
try
let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "%s" custom format string.
let format = "%s"
try
let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
printfn $"'{intervalString}' ({format}) --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// 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仅当 format
是值为“g”或“G”的标准TimeSpan格式字符串时, 方法才使用 参数formatProvider
指定的区域性的约定。 “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)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 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)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 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-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
open System
open System.Globalization
let inputs = [| "3"; "16:42"; "1:6:52:35.0625"; "1:6:52:35,0625" |]
let formats = [| "g"; "G"; "%h" |]
let culture = CultureInfo "fr-FR"
// Parse each string in inputs using formats and the fr-FR culture.
for input in inputs do
try
let interval = TimeSpan.ParseExact(input, formats, culture)
printfn $"{input} --> {interval:c}"
with
| :? FormatException ->
printfn $"{input} --> Bad Format"
| :? OverflowException ->
printfn $"{input} --> Overflow"
// 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
,分析操作才能成功。 分析操作尝试从数组中的formats
第一个元素开始匹配input
到中的每个元素。
重要
ParseExact仅当用于分析input
的格式字符串是值为“g”或“G”的标准TimeSpan格式字符串时,方法才使用 参数formatProvider
指定的区域性的约定。 “c”、“t”和“T”标准格式字符串使用固定区域性的格式设置约定。 自定义格式字符串定义输入字符串的精确格式,并使用文本字符分隔时间间隔的组件。
如果用于分析input
的格式字符串是标准格式字符串,则 formatProvider
参数是一个IFormatProvider实现,它提供有关返回的字符串格式的区域性特定信息。 参数 formatProvider
可以是以下任一参数:
一个 CultureInfo 对象,表示其格式约定将反映在返回的字符串中的区域性。 DateTimeFormatInfo属性返回CultureInfo.DateTimeFormat的对象定义返回的字符串的格式。
一个 DateTimeFormatInfo 对象,该对象定义返回的字符串的格式。
实现 接口的 IFormatProvider 自定义对象。 其 IFormatProvider.GetFormat 方法返回一个 DateTimeFormatInfo 对象,该对象提供格式设置信息。
如果 formatProvider
为 null
,则 DateTimeFormatInfo 使用与当前区域性关联的 对象。
另请参阅
适用于
ParseExact(String, String, IFormatProvider)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 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
open System
open System.Globalization
do
// Parse hour:minute value with "g" specifier current culture.
let intervalString = "17:14"
let format = "g"
let culture = CultureInfo.CurrentCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse hour:minute:second value with "G" specifier.
let intervalString = "17:14:48"
let format = "G"
let culture = CultureInfo.InvariantCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse hours:minute.second value with "G" specifier
// and current (en-US) culture.
let intervalString = "17:14:48.153"
let format = "G"
let culture = CultureInfo.CurrentCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
let intervalString = "3:17:14:48.153"
let format = "G"
let culture = CultureInfo.CurrentCulture
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
let intervalString = "3:17:14:48.153"
let format = "G"
let culture = CultureInfo "fr-FR"
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
let intervalString = "3:17:14:48,153"
let format = "G"
try
let interval = TimeSpan.ParseExact(intervalString, format, culture)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "c" standard format string.
let intervalString = "12"
let format = "c"
try
let interval = TimeSpan.ParseExact(intervalString, format, null)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "%h" custom format string.
let format = "%h"
try
let interval = TimeSpan.ParseExact(intervalString, format, null)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// Parse a single number using the "%s" custom format string.
let format = "%s"
try
let interval = TimeSpan.ParseExact(intervalString, format, null)
printfn $"'{intervalString}' --> {interval}"
with
| :? FormatException ->
printfn $"'{intervalString}': Bad Format for '{format}'"
| :? OverflowException ->
printfn $"'{intervalString}': Overflow"
// 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 格式字符串。
重要
仅当 是值为“g”或“G”的标准TimeSpan格式字符串时format
,方法ParseExact才使用 参数formatProvider
指定的区域性的约定。 “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)
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
- Source:
- TimeSpan.cs
使用指定的格式和区域性特定格式信息,将时间间隔的字符型范围转换为其等效的 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
指定。
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈