TimeSpan.ParseExact Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem angegebenen Format genau entsprechen.
Überlädt
ParseExact(String, String, IFormatProvider, TimeSpanStyles) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen. |
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung der angegebenen Formate, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen. |
ParseExact(String, String[], IFormatProvider, TimeSpanStyles) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung der angegebenen Formate, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen. |
ParseExact(String, String[], IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Arrays von Formatierungszeichenfolgen und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen. |
ParseExact(String, String, IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen. |
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) |
Konvertiert die Zeichenspanne eines Zeitintervalls unter Verwendung des angegebenen Formats und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen. |
ParseExact(String, String, IFormatProvider, TimeSpanStyles)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.
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
Parameter
- input
- String
Eine Zeichenfolge, die das zu konvertierende Zeitintervall angibt.
- format
- String
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der input
vorhanden sein können.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
, formatProvider
und styles
angegeben.
Ausnahmen
styles
ist ein ungültiger TimeSpanStyles-Wert.
input
ist null
.
Das Format von input
ist ungültig.
input
stellt eine Zahl dar, die kleiner als TimeSpan.MinValue oder größer als TimeSpan.MaxValue ist.
- oder -
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.
Beispiele
Im folgenden Beispiel wird die ParseExact(String, String, IFormatProvider) -Methode verwendet, um mehrere Zeichenfolgendarstellungen von Zeitintervallen mithilfe verschiedener Formatzeichenfolgen und Kulturen zu analysieren. Außerdem wird der TimeSpanStyles.AssumeNegative Wert verwendet, um jede Zeichenfolge als negatives Zeitintervall zu interpretieren. Die Ausgabe des Beispiels veranschaulicht, dass sich der TimeSpanStyles.AssumeNegative Stil nur auf den Rückgabewert auswirkt, wenn er mit benutzerdefinierten Formatzeichenfolgen verwendet wird.
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
Hinweise
Die ParseExact -Methode analysiert die Zeichenfolgendarstellung eines Zeitintervalls, das im vom -Parameter definierten Format vorliegen muss, mit der format
Ausnahme, dass führende und nachfolgende Leerzeichen ignoriert werden. Da input
dem Format von format
genau entsprechen muss, sollten Sie beim Konvertieren einer Zeichenfolgeneingabe durch den Benutzer in ein Zeitintervall immer die Ausnahmebehandlung verwenden. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) -Methode aufrufen.
Der format
Parameter ist eine Zeichenfolge, die entweder einen einzelnen Standardformatbezeichner oder einen oder mehrere benutzerdefinierte Formatbezeichner enthält, die das erforderliche Format von input
definieren. Weitere Informationen zu gültigen Formatzeichenfolgen finden Sie unter Standard-TimeSpan-Formatzeichenfolgen und benutzerdefinierte TimeSpan-Formatzeichenfolgen.
Wichtig
Die ParseExact -Methode verwendet die Konventionen der vom formatProvider
-Parameter angegebenen Kultur nur, wenn format
es sich um eine Standardformatzeichenfolge TimeSpan handelt, deren Wert entweder "g" oder "G" ist. Die Standardformatzeichenfolgen "c", "t" und "T" verwenden die Formatierungskonventionen der invarianten Kultur. Benutzerdefinierte Formatzeichenfolgen definieren das genaue Format der Eingabezeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls zu trennen.
Der formatProvider
Parameter ist eine IFormatProvider Implementierung, die kulturspezifische Informationen über das Format der zurückgegebenen Zeichenfolge bereitstellt, wenn format
es sich um eine Standardformatzeichenfolge handelt. Der formatProvider
Parameter kann einer der folgenden Sein:
Ein CultureInfo -Objekt, das die Kultur darstellt, deren Formatierungskonventionen in der zurückgegebenen Zeichenfolge widergespiegelt werden sollen. Das DateTimeFormatInfo von der CultureInfo.DateTimeFormat -Eigenschaft zurückgegebene Objekt definiert die Formatierung der zurückgegebenen Zeichenfolge.
Ein DateTimeFormatInfo -Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.
Ein benutzerdefiniertes Objekt, das die IFormatProvider -Schnittstelle implementiert. Die - IFormatProvider.GetFormat Methode gibt ein DateTimeFormatInfo -Objekt zurück, das Formatierungsinformationen bereitstellt.
Wenn formatProvider
ist null
, wird das DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.
Der styles
Parameter wirkt sich auf die Interpretation von Zeichenfolgen aus, die mit benutzerdefinierten Formatzeichenfolgen analysiert werden. Sie bestimmt, ob input
nur dann als negatives Zeitintervall interpretiert wird, wenn ein negatives Vorzeichen vorhanden ist (TimeSpanStyles.None) oder ob es immer als negatives Zeitintervall (TimeSpanStyles.AssumeNegative) interpretiert wird. Wenn TimeSpanStyles.AssumeNegative nicht verwendet wird, format
muss ein literales negatives Vorzeichensymbol (z. B. "\-") eingeschlossen werden, um ein negatives Zeitintervall erfolgreich zu analysieren.
Weitere Informationen
Gilt für:
ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung der angegebenen Formate, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.
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
Parameter
- input
- ReadOnlySpan<Char>
Eine Spanne, die das zu konvertierende Zeitintervall angibt.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, die das erforderliche Format von input
definieren.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der Eingabe vorhanden sein können.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
, formatProvider
und styles
angegeben.
Gilt für:
ParseExact(String, String[], IFormatProvider, TimeSpanStyles)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung der angegebenen Formate, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.
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
Parameter
- input
- String
Eine Zeichenfolge, die das zu konvertierende Zeitintervall angibt.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, die das erforderliche Format von input
definieren.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der Eingabe vorhanden sein können.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
, formatProvider
und styles
angegeben.
Ausnahmen
styles
ist ein ungültiger TimeSpanStyles-Wert.
input
ist null
.
Das Format von input
ist ungültig.
input
stellt eine Zahl dar, die kleiner als TimeSpan.MinValue oder größer als TimeSpan.MaxValue ist.
- oder -
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.
Beispiele
Im folgenden Beispiel wird die ParseExact(String, String[], IFormatProvider, TimeSpanStyles) -Methode aufgerufen, um jedes Element eines Zeichenfolgenarrays in einen TimeSpan Wert zu konvertieren. Die Zeichenfolgen können ein Zeitintervall entweder im allgemeinen Kurzformat oder im allgemeinen langen Format darstellen.
Darüber hinaus ändert das Beispiel die Art und Weise, wie die Methoden der Zeitintervallanalyse eine einzelne Ziffer interpretieren. Normalerweise wird eine einzelne Ziffer als Anzahl von Tagen in einem Zeitintervall interpretiert. Stattdessen wird die %h
benutzerdefinierte Formatzeichenfolge verwendet, um eine einzelne Ziffer als Anzahl von Stunden zu interpretieren. Beachten Sie, dass die %h
benutzerdefinierte Formatzeichenfolge den anderen Formatzeichenfolgen im formats
Array vorangestellt werden muss, damit diese Änderung wirksam ist. Beachten Sie außerdem in der Ausgabe, dass das TimeSpanStyles.AssumeNegative im Methodenaufruf angegebene Flag nur beim Analysieren einer Zeichenfolge mit diesem Formatbezeichner verwendet wird.
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
Hinweise
Die ParseExact(String, String[], IFormatProvider, TimeSpanStyles) -Methode analysiert die Zeichenfolgendarstellung eines Zeitintervalls, das in einem der vom -Parameter definierten Formate vorliegen muss, mit der formats
Ausnahme, dass führende und nachfolgende Leerzeichen ignoriert werden. Da input
genau einem der in formats
angegebenen Formate entsprechen muss, sollten Sie beim Konvertieren einer Zeichenfolgeneingabe durch den Benutzer in ein Zeitintervall immer die Ausnahmebehandlung verwenden. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) -Methode aufrufen.
Der formats
Parameter ist ein Zeichenfolgenarray, dessen Elemente entweder aus einem einzelnen Standardformatbezeichner oder einem oder mehreren benutzerdefinierten Formatbezeichnern bestehen, die das erforderliche Format von input
definieren. Weitere Informationen zu gültigen Formatzeichenfolgen finden Sie unter Standard-TimeSpan-Formatzeichenfolgen und benutzerdefinierte TimeSpan-Formatzeichenfolgen. input
muss genau einem Member von formats
entsprechen, damit der Analysevorgang erfolgreich ist. Der Analysevorgang versucht, input
die Übereinstimmung mit jedem Element in formats
beginnend mit dem ersten Element im Array zu erstellen.
Wichtig
Die ParseExact -Methode verwendet die Konventionen der vom formatProvider
-Parameter angegebenen Kultur nur, wenn die zum Analysieren input
verwendete Formatzeichenfolge eine Standardformatzeichenfolge TimeSpan ist, deren Wert entweder "g" oder "G" ist. Die Standardformatzeichenfolgen "c", "t" und "T" verwenden die Formatierungskonventionen der invarianten Kultur. Benutzerdefinierte Formatzeichenfolgen definieren das genaue Format der Eingabezeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls zu trennen.
Der formatProvider
Parameter ist eine IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn die zum Analysieren input
verwendete Formatzeichenfolge eine Standardformatzeichenfolge ist. Der formatProvider
Parameter kann einer der folgenden Sein:
Ein CultureInfo -Objekt, das die Kultur darstellt, deren Formatierungskonventionen in der zurückgegebenen Zeichenfolge widergespiegelt werden sollen. Das DateTimeFormatInfo von der CultureInfo.DateTimeFormat -Eigenschaft zurückgegebene Objekt definiert die Formatierung der zurückgegebenen Zeichenfolge.
Ein DateTimeFormatInfo -Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert. Die - IFormatProvider.GetFormat Methode gibt ein DateTimeFormatInfo -Objekt zurück, das Formatierungsinformationen bereitstellt.
Wenn formatProvider
ist null
, wird das DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.
Der styles
Parameter wirkt sich auf die Interpretation von Zeichenfolgen aus, die mit benutzerdefinierten Formatzeichenfolgen analysiert werden. Sie bestimmt, ob input
nur dann als negatives Zeitintervall interpretiert wird, wenn ein negatives Vorzeichen vorhanden ist (TimeSpanStyles.None), oder ob es immer als negatives Zeitintervall (TimeSpanStyles.AssumeNegative) interpretiert wird. Wenn TimeSpanStyles.AssumeNegative nicht verwendet wird, format
muss ein negatives Literalzeichenzeichen (z. B. "\-") enthalten, um ein negatives Zeitintervall erfolgreich zu analysieren.
Weitere Informationen
Gilt für:
ParseExact(String, String[], IFormatProvider)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Arrays von Formatierungszeichenfolgen und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.
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
Parameter
- input
- String
Eine Zeichenfolge, die das zu konvertierende Zeitintervall angibt.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, das das erforderliche Format von input
definiert.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
und formatProvider
angegeben.
Ausnahmen
input
ist null
.
Das Format von input
ist ungültig.
input
stellt eine Zahl dar, die kleiner als TimeSpan.MinValue oder größer als TimeSpan.MaxValue ist.
- oder -
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.
Beispiele
Im folgenden Beispiel wird die ParseExact(String, String[], IFormatProvider) -Methode aufgerufen, um jedes Element eines Zeichenfolgenarrays in einen TimeSpan Wert zu konvertieren. Das Beispiel interpretiert die Zeichenfolgen mithilfe der Formatierungskonventionen der Kultur Französisch -Frankreich ("fr-FR"). Die Zeichenfolgen können ein Zeitintervall im allgemeinen Kurzformat oder im allgemeinen Langformat darstellen.
Darüber hinaus ändert das Beispiel die Art und Weise, in der die Methoden der Zeitintervallanalyse eine einzelne Ziffer interpretieren. Normalerweise wird eine einzelne Ziffer als die Anzahl von Tagen in einem Zeitintervall interpretiert. Stattdessen wird die %h
benutzerdefinierte Formatzeichenfolge verwendet, um eine einzelne Ziffer als Anzahl von Stunden zu interpretieren. Beachten Sie, dass die %h
benutzerdefinierte Formatzeichenfolge den anderen Formatzeichenfolgen im formats
Array vorangestellt werden muss, damit diese Änderung wirksam ist.
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
Hinweise
Die ParseExact(String, String, IFormatProvider) -Methode analysiert die Zeichenfolgendarstellung eines Zeitintervalls, das sich in einem der vom Parameter definierten Formate befinden muss, mit dem formats
Unterschied, dass führende und nachfolgende Leerzeichen ignoriert werden. Da input
genau einem der in formats
angegebenen Formate entsprechen muss, sollten Sie beim Konvertieren einer Zeichenfolgeneingabe durch den Benutzer in ein Zeitintervall immer die Ausnahmebehandlung verwenden. Wenn Sie die Ausnahmebehandlung lieber nicht verwenden möchten, können Sie stattdessen die TryParseExact(String, String[], IFormatProvider, TimeSpan) -Methode aufrufen.
Der formats
Parameter ist ein Zeichenfolgenarray, dessen Elemente entweder aus einem einzelnen Standardformatbezeichner oder aus einem oder mehreren benutzerdefinierten Formatbezeichnern bestehen, die das erforderliche Format von input
definieren. Weitere Informationen zu gültigen Formatzeichenfolgen finden Sie unter Standard-TimeSpan-Formatzeichenfolgen und benutzerdefinierte TimeSpan-Formatzeichenfolgen. input
muss genau einem Member von formats
entsprechen, damit der Analysevorgang erfolgreich ist. Der Analysevorgang versucht, mit jedem Element formats
ab dem ersten Element im Array abzugleicheninput
.
Wichtig
Die ParseExact -Methode verwendet die Konventionen der kultur, die durch den formatProvider
-Parameter angegeben werden, nur, wenn die zum Analysieren input
verwendete Formatzeichenfolge eine Standardformatzeichenfolge TimeSpan ist, deren Wert entweder "g" oder "G" ist. Die Standardformatzeichenfolgen "c", "t" und "T" verwenden die Formatierungskonventionen der invarianten Kultur. Benutzerdefinierte Formatzeichenfolgen definieren das genaue Format der Eingabezeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls zu trennen.
Der formatProvider
Parameter ist eine IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn die zum Analysieren input
verwendete Formatzeichenfolge eine Standardformatzeichenfolge ist. Der formatProvider
Parameter kann einer der folgenden Sein:
Ein CultureInfo -Objekt, das die Kultur darstellt, deren Formatierungskonventionen in der zurückgegebenen Zeichenfolge widergespiegelt werden sollen. Das DateTimeFormatInfo von der CultureInfo.DateTimeFormat -Eigenschaft zurückgegebene Objekt definiert die Formatierung der zurückgegebenen Zeichenfolge.
Ein DateTimeFormatInfo -Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert. Die - IFormatProvider.GetFormat Methode gibt ein DateTimeFormatInfo -Objekt zurück, das Formatierungsinformationen bereitstellt.
Wenn formatProvider
ist null
, wird das DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.
Weitere Informationen
- TryParseExact(String, String[], IFormatProvider, TimeSpan)
- TimeSpan-Standardformatzeichenfolgen
- Benutzerdefinierte TimeSpan-Formatzeichenfolgen
Gilt für:
ParseExact(String, String, IFormatProvider)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.
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
Parameter
- input
- String
Eine Zeichenfolge, die das zu konvertierende Zeitintervall angibt.
- format
- String
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
und formatProvider
angegeben.
Ausnahmen
input
ist null
.
Das Format von input
ist ungültig.
input
stellt eine Zahl dar, die kleiner als TimeSpan.MinValue oder größer als TimeSpan.MaxValue ist.
- oder -
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.
Beispiele
Im folgenden Beispiel wird die ParseExact(String, String, IFormatProvider) -Methode verwendet, um mehrere Zeichenfolgendarstellungen von Zeitintervallen mit verschiedenen Formatzeichenfolgen und Kulturen zu analysieren.
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
Hinweise
Die ParseExact(String, String, IFormatProvider) -Methode analysiert die Zeichenfolgendarstellung eines Zeitintervalls, das in dem vom -Parameter definierten Format vorliegen muss, mit der format
Ausnahme, dass führende und nachfolgende Leerzeichen ignoriert werden. Da input
genau dem Format von format
entsprechen muss, sollten Sie beim Konvertieren einer Zeichenfolgeneingabe durch den Benutzer in ein Zeitintervall immer die Ausnahmebehandlung verwenden. Wenn Sie die Ausnahmebehandlung lieber nicht verwenden möchten, können Sie stattdessen die TryParseExact(String, String, IFormatProvider, TimeSpan) -Methode aufrufen.
Der format
Parameter ist eine Zeichenfolge, die entweder einen einzelnen Standardformatbezeichner oder einen oder mehrere benutzerdefinierte Formatbezeichner enthält, die das erforderliche Format von input
definieren. Weitere Informationen zu gültigen Formatzeichenfolgen finden Sie unter Standard-TimeSpan-Formatzeichenfolgen und benutzerdefinierte TimeSpan-Formatzeichenfolgen.
Wichtig
Die ParseExact -Methode verwendet die Konventionen der vom formatProvider
-Parameter angegebenen Kultur nur, wenn es sich um format
eine Standardformatzeichenfolge TimeSpan handelt, deren Wert entweder "g" oder "G" ist. Die Standardformatzeichenfolgen "c", "t" und "T" verwenden die Formatierungskonventionen der invarianten Kultur. Benutzerdefinierte Formatzeichenfolgen definieren das genaue Format der Eingabezeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls zu trennen.
Der formatProvider
Parameter ist eine IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn format
es sich um eine Standardformatzeichenfolge handelt. Der formatProvider
Parameter kann einer der folgenden Sein:
Ein CultureInfo -Objekt, das die Kultur darstellt, deren Formatierungskonventionen in der zurückgegebenen Zeichenfolge widergespiegelt werden sollen. Das DateTimeFormatInfo von der CultureInfo.DateTimeFormat -Eigenschaft zurückgegebene Objekt definiert die Formatierung der zurückgegebenen Zeichenfolge.
Ein DateTimeFormatInfo -Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert. Die - IFormatProvider.GetFormat Methode gibt ein DateTimeFormatInfo -Objekt zurück, das Formatierungsinformationen bereitstellt.
Wenn formatProvider
ist null
, wird das DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.
Weitere Informationen
Gilt für:
ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)
Konvertiert die Zeichenspanne eines Zeitintervalls unter Verwendung des angegebenen Formats und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.
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
Parameter
- input
- ReadOnlySpan<Char>
Eine Spanne, die das zu konvertierende Zeitintervall angibt.
- format
- ReadOnlySpan<Char>
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der input
vorhanden sein können.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
und formatProvider
angegeben.
Gilt für:
Feedback
Feedback senden und anzeigen für