TimeSpan.ParseExact Methode
Definition
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls in die entsprechende TimeSpan.Converts the string representation of a time interval to its TimeSpan equivalent. Das Format der Zeichenfolgendarstellung muss einem angegebenen Format genau entsprechen.The format of the string representation must match a specified format exactly.
Ü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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly. |
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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly. |
ParseExact(String, String[], IFormatProvider, TimeSpanStyles) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung der angegebenen Formate, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly. |
ParseExact(String, String[], IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Arrays von Formatierungszeichenfolgen und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly. |
ParseExact(String, String, IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats und der kulturspezifischen Formatierungsinformationen in die entsprechende TimeSpan.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly. |
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.Converts the char span of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly. |
ParseExact(String, String, IFormatProvider, TimeSpanStyles)
Konvertiert die Zeichenfolgendarstellung eines Zeitintervalls unter Verwendung des angegebenen Formats, der kulturspezifischen Formatierungsinformationen und Stile in die entsprechende TimeSpan.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly.
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.A string that specifies the time interval to convert.
- format
- String
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.A standard or custom format string that defines the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der input
vorhanden sein können.A bitwise combination of enumeration values that defines the style elements that may be present in input
.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
, formatProvider
und styles
angegeben.A time interval that corresponds to input
, as specified by format
, formatProvider
, and styles
.
Ausnahmen
styles
ist ein ungültiger TimeSpanStyles-Wert.styles
is an invalid TimeSpanStyles value.
input
ist null
.input
is null
.
Das Format von input
ist ungültig.input
has an invalid format.
input
stellt eine Zahl dar, die kleiner als MinValue oder größer als MaxValue ist.input
represents a number that is less than MinValue or greater than MaxValue.
- oder --or-
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.At least one of the days, hours, minutes, or seconds components in input
is outside its valid range.
Beispiele
Im folgenden Beispiel wird die- ParseExact(String, String, IFormatProvider) Methode verwendet, um mehrere Zeichen folgen Darstellungen von Zeitintervallen mithilfe verschiedener Format Zeichenfolgen und Kulturen zu analysieren.The following example uses the ParseExact(String, String, IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures. Außerdem wird der- TimeSpanStyles.AssumeNegative Wert verwendet, um jede Zeichenfolge als negatives Zeitintervall zu interpretieren.It also uses the TimeSpanStyles.AssumeNegative value to interpret each string as a negative time interval. Die Ausgabe des Beispiels veranschaulicht, dass der TimeSpanStyles.AssumeNegative Stil den Rückgabewert nur dann beeinflusst, wenn er mit benutzerdefinierten Format Zeichenfolgen verwendet wird.The output from the example illustrates that the TimeSpanStyles.AssumeNegative style affects the return value only when it is used with custom format strings.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture = null;
// Parse hour:minute value with custom format specifier.
intervalString = "17:14";
format = "h\\:mm";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hour:minute:second value with "g" specifier.
intervalString = "17:14:48";
format = "g";
culture = CultureInfo.InvariantCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hours:minute.second value with custom format specifier.
intervalString = "17:14:48.153";
format = @"h\:mm\:ss\.fff";
culture = null;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with a custom format specifier.
intervalString = "3:17:14:48.153";
format = @"d\:hh\:mm\:ss\.fff";
culture = null;
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
culture = new CultureInfo("fr-FR");
try {
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%h" custom format string.
format = "%h";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%s" custom format string.
format = "%s";
try {
interval = TimeSpan.ParseExact(intervalString, format,
null, TimeSpanStyles.AssumeNegative);
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
}
}
// The example displays the following output:
// '17:14' (h\:mm) --> -17:14:00
// '17:14:48' (g) --> 17:14:48
// '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
// '3:17:14:48.153' (G) --> 3.17:14:48.1530000
// '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
// '3:17:14:48,153' (G) --> 3.17:14:48.1530000
// '12' (c) --> 12.00:00:00
// '12' (%h) --> -12:00:00
// '12' (%s) --> -00:00:12
Imports System.Globalization
Module Example
Public Sub Main()
Dim intervalString, format As String
Dim interval As TimeSpan
Dim culture As CultureInfo = Nothing
' Parse hour:minute value with custom format specifier.
intervalString = "17:14"
format = "h\:mm"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hour:minute:second value with "g" specifier.
intervalString = "17:14:48"
format = "g"
culture = CultureInfo.InvariantCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hours:minute.second value with custom format specifier.
intervalString = "17:14:48.153"
format = "h\:mm\:ss\.fff"
culture = Nothing
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with a custom format specifier.
intervalString = "3:17:14:48.153"
format = "d\:hh\:mm\:ss\.fff"
culture = Nothing
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48,153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "c" standard format string.
intervalString = "12"
format = "c"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%h" custom format string.
format = "%h"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%s" custom format string.
format = "%s"
Try
interval = TimeSpan.ParseExact(intervalString, format,
Nothing, TimeSpanStyles.AssumeNegative)
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
End Sub
End Module
' The example displays the following output:
' '17:14' (h\:mm) --> -17:14:00
' '17:14:48' (g) --> 17:14:48
' '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
' '3:17:14:48.153' (G) --> 3.17:14:48.1530000
' '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
' '3:17:14:48,153' (G) --> 3.17:14:48.1530000
' '12' (c) --> 12.00:00:00
' '12' (%h) --> -12:00:00
' '12' (%s) --> -00:00:12
Hinweise
Die- ParseExact Methode analysiert die Zeichen folgen Darstellung eines Zeitintervalls, das in dem vom-Parameter definierten Format vorliegen muss, mit dem Unterschied format
, dass führende und nachfolgende Leerzeichen ignoriert werden.The ParseExact method parses the string representation of a time interval, which must be in the format defined by the format
parameter, except that leading and trailing white-space characters are ignored. Da input
dem Format genau entsprechen muss format
, sollten Sie immer die Ausnahmebehandlung verwenden, wenn Sie eine Zeichen folgen Eingabe vom Benutzer in ein Zeitintervall umwandelt.Because input
must conform to the format of format
exactly, you should always use exception handling when converting a string input by the user to a time interval. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die-Methode aufzurufen TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) .If you prefer not to use exception handling, you can call the TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) method instead.
Der- format
Parameter ist eine Zeichenfolge, die entweder einen einzelnen Standardformat Bezeichner oder einen oder mehrere benutzerdefinierte Format Bezeichner enthält, die das erforderliche Format von definieren input
.The format
parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of input
. Weitere Informationen zu gültigen Format Zeichenfolgen finden Sie unter TimeSpan-Standard Format Zeichenfolgen und Benutzerdefinierte TimeSpan-FormatZeichenfolgenFor more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.
Wichtig
Die- ParseExact Methode verwendet die Konventionen der Kultur, die durch den- formatProvider
Parameter angegeben wird, nur, wenn format
eine Standard TimeSpan Format Zeichenfolge ist, deren Wert entweder "g" oder "g" ist.The ParseExact method uses the conventions of the culture specified by the formatProvider
parameter only if format
is a standard TimeSpan format string whose value is either "g" or "G". Die Standardformat Zeichenfolgen "c", "t" und "t" verwenden die Formatierungs Konventionen der invarianten Kultur.The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Benutzerdefinierte Format Zeichenfolgen definieren das genaue Format der Eingabe Zeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls voneinander zu trennen.Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.
Der- formatProvider
Parameter ist eine- IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn format
eine Standardformat Zeichenfolge ist.The formatProvider
parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if format
is a standard format string. Der- formatProvider
Parameter kann eine der folgenden sein:The formatProvider
parameter can be any of the following:
Ein- CultureInfo Objekt, das die Kultur darstellt, deren Formatierungs Konventionen in der zurückgegebenen Zeichenfolge reflektiert werden sollen.A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. Das DateTimeFormatInfo von der-Eigenschaft zurückgegebene-Objekt CultureInfo.DateTimeFormat definiert die Formatierung der zurückgegebenen Zeichenfolge.The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
Ein- DateTimeFormatInfo Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.A DateTimeFormatInfo object that defines the formatting of the returned string.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert.A custom object that implements the IFormatProvider interface. Die IFormatProvider.GetFormat Methode gibt ein- DateTimeFormatInfo Objekt zurück, das Formatierungsinformationen bereitstellt.Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
Wenn formatProvider
null
den Wert hat, wird das- DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.If formatProvider
is null
, the DateTimeFormatInfo object that is associated with the current culture is used.
Der- styles
Parameter beeinflusst die Interpretation von Zeichen folgen, die mit benutzerdefinierten Format Zeichenfolgen analysiert werden.The styles
parameter affects the interpretation of strings that are parsed using custom format strings. Es bestimmt input
, ob nur als negatives Zeitintervall interpretiert wird, wenn ein negatives Vorzeichen () vorhanden ist TimeSpanStyles.None , oder ob es immer als negatives Zeitintervall () interpretiert wird TimeSpanStyles.AssumeNegative .It determines whether input
is interpreted as a negative time interval only if a negative sign is present (TimeSpanStyles.None), or whether it is always interpreted as a negative time interval (TimeSpanStyles.AssumeNegative). Wenn TimeSpanStyles.AssumeNegative nicht verwendet wird, format
muss ein literales negatives Vorzeichen (z. b. " \ -") enthalten, um ein negatives Zeitintervall erfolgreich zu analysieren.If TimeSpanStyles.AssumeNegative is not used, format
must include a literal negative sign symbol (such as "\-") to successfully parse a negative time interval.
Weitere Informationen
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly.
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.A span that specifies the time interval to convert.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, die das erforderliche Format von input
definieren.An array of standard or custom format strings that define the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der Eingabe vorhanden sein können.A bitwise combination of enumeration values that defines the style elements that may be present in input.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
, formatProvider
und styles
angegeben.A time interval that corresponds to input
, as specified by formats
, formatProvider
, and styles
.
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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly.
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.A string that specifies the time interval to convert.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, die das erforderliche Format von input
definieren.An array of standard or custom format strings that define the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der Eingabe vorhanden sein können.A bitwise combination of enumeration values that defines the style elements that may be present in input.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
, formatProvider
und styles
angegeben.A time interval that corresponds to input
, as specified by formats
, formatProvider
, and styles
.
Ausnahmen
styles
ist ein ungültiger TimeSpanStyles-Wert.styles
is an invalid TimeSpanStyles value.
input
ist null
.input
is null
.
Das Format von input
ist ungültig.input
has an invalid format.
input
stellt eine Zahl dar, die kleiner als MinValue oder größer als MaxValue ist.input
represents a number that is less than MinValue or greater than MaxValue.
- oder --or-
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.At least one of the days, hours, minutes, or seconds components in input
is outside its valid range.
Beispiele
Im folgenden Beispiel wird die- ParseExact(String, String[], IFormatProvider, TimeSpanStyles) Methode aufgerufen, um die einzelnen Elemente eines Zeichen folgen Arrays in einen-Wert zu konvertieren TimeSpan .The following example calls the ParseExact(String, String[], IFormatProvider, TimeSpanStyles) method to convert each element of a string array to a TimeSpan value. Die Zeichen folgen können ein Zeitintervall mit dem allgemeinen Kurzformat oder dem allgemeinen langen Format darstellen.The strings can represent a time interval in either the general short format or the general long format.
Außerdem wird in diesem Beispiel die Art und Weise geändert, in der die Methoden zum Analysieren von Zeitintervallen eine einzelne Ziffer interpretieren.In addition, the example changes the way in which the time interval parsing methods interpret a single digit. Normalerweise wird eine einzelne Ziffer als Anzahl von Tagen in einem Zeitintervall interpretiert.Ordinarily, a single digit is interpreted as the number of days in a time interval. Stattdessen wird die %h
benutzerdefinierte Format Zeichenfolge verwendet, um eine einzelne Ziffer als Anzahl von Stunden zu interpretieren.Instead, the %h
custom format string is used to interpret a single digit as the number of hours. Damit diese Änderung wirksam wird, müssen Sie beachten, dass die %h
benutzerdefinierte Format Zeichenfolge den anderen Format Zeichenfolgen im Array vorangestellt ist formats
.For this change to be effective, note that the %h
custom format string must precede the other format strings in the formats
array. Beachten Sie auch, dass das TimeSpanStyles.AssumeNegative im Methoden aufrufszeichen angegebene Flag nur verwendet wird, wenn eine Zeichenfolge mit diesem Format Bezeichner verglichen wird.Also note from the output that the TimeSpanStyles.AssumeNegative flag specified in the method call is used only when parsing a string with this format specifier.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "%h", "g", "G" };
TimeSpan interval;
CultureInfo culture = new CultureInfo("de-DE");
// Parse each string in inputs using formats and the de-DE culture.
foreach (string input in inputs) {
try {
interval = TimeSpan.ParseExact(input, formats, culture,
TimeSpanStyles.AssumeNegative);
Console.WriteLine("{0} --> {1:c}", input, interval);
}
catch (FormatException) {
Console.WriteLine("{0} --> Bad Format", input);
}
catch (OverflowException) {
Console.WriteLine("{0} --> Overflow", input);
}
}
}
}
// The example displays the following output:
// 3 --> -03:00:00
// 16:42 --> 16:42:00
// 1:6:52:35.0625 --> Bad Format
// 1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization
Module Example
Public Sub Main()
Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" }
Dim formats() As String = { "%h", "g", "G" }
Dim interval As TimeSpan
Dim culture As New CultureInfo("de-DE")
' Parse each string in inputs using formats and the de-DE culture.
For Each input As String In inputs
Try
interval = TimeSpan.ParseExact(input, formats, culture,
TimeSpanStyles.AssumeNegative)
Console.WriteLine("{0} --> {1:c}", input, interval)
Catch e As FormatException
Console.WriteLine("{0} --> Bad Format", input)
Catch e As OverflowException
Console.WriteLine("{0} --> Overflow", input)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 3 --> -03:00:00
' 16:42 --> 16:42:00
' 1:6:52:35.0625 --> Bad Format
' 1:6:52:35,0625 --> 1.06:52:35.0625000
Hinweise
Die- ParseExact(String, String[], IFormatProvider, TimeSpanStyles) Methode analysiert die Zeichen folgen Darstellung eines Zeitintervalls, das in einem der durch den-Parameter definierten Formate vorliegen muss, mit dem Unterschied formats
, dass führende und nachfolgende Leerzeichen ignoriert werden.The ParseExact(String, String[], IFormatProvider, TimeSpanStyles) method parses the string representation of a time interval, which must be in one of the formats defined by the formats
parameter, except that leading and trailing white-space characters are ignored. Da input
genau einem der in angegebenen Formate entsprechen muss formats
, sollten Sie immer die Ausnahmebehandlung verwenden, wenn Sie eine Zeichen folgen Eingabe vom Benutzer in ein Zeitintervall umwandelt.Because input
must exactly conform to one of the formats specified in formats
, you should always use exception handling when converting a string input by the user to a time interval. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die-Methode aufzurufen TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) .If you prefer not to use exception handling, you can call the TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) method instead.
Der- formats
Parameter ist ein Zeichen folgen Array, dessen Elemente entweder aus einem einzelnen Standardformat Bezeichner bestehen, oder aus einem oder mehreren benutzerdefinierten Format Bezeichnern, die das erforderliche Format von definieren input
.The formats
parameter is a string array whose elements consist of either a single standard format specifier, or one or more custom format specifiers that define the required format of input
. Weitere Informationen zu gültigen Format Zeichenfolgen finden Sie unter TimeSpan-Standard Format Zeichenfolgen und Benutzerdefinierte TimeSpan-FormatZeichenfolgenFor more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings. input
muss genau einem Member von entsprechen formats
, damit der Analyse Vorgang erfolgreich durchgeführt werden konnte.input
must correspond exactly to a member of formats
for the parse operation to succeed. Der Analyse Vorgang versucht, input
mit jedem Element in abzugleichen, formats
beginnend mit dem ersten Element im Array.The parse operation attempts to match input
to each element in formats
starting with the first element in the array.
Wichtig
Die- ParseExact Methode verwendet die Konventionen der Kultur, die durch den-Parameter angegeben wird formatProvider
, nur dann, wenn die zum analysieren verwendete Format Zeichenfolge input
eine Standard TimeSpan Format Zeichenfolge ist, deren Wert entweder "g" oder "g" ist.The ParseExact method uses the conventions of the culture specified by the formatProvider
parameter only if the format string used to parse input
is a standard TimeSpan format string whose value is either "g" or "G". Die Standardformat Zeichenfolgen "c", "t" und "t" verwenden die Formatierungs Konventionen der invarianten Kultur.The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Benutzerdefinierte Format Zeichenfolgen definieren das genaue Format der Eingabe Zeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls voneinander zu trennen.Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.
Der- formatProvider
Parameter ist eine- IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn die zum analysieren verwendete Format Zeichenfolge input
eine Standardformat Zeichenfolge ist.The formatProvider
parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if the format string used to parse input
is a standard format string. Der- formatProvider
Parameter kann eine der folgenden sein:The formatProvider
parameter can be any of the following:
Ein- CultureInfo Objekt, das die Kultur darstellt, deren Formatierungs Konventionen in der zurückgegebenen Zeichenfolge reflektiert werden sollen.A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. Das DateTimeFormatInfo von der-Eigenschaft zurückgegebene-Objekt CultureInfo.DateTimeFormat definiert die Formatierung der zurückgegebenen Zeichenfolge.The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
Ein- DateTimeFormatInfo Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.A DateTimeFormatInfo object that defines the formatting of the returned string.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert.A custom object that implements the IFormatProvider interface. Die IFormatProvider.GetFormat Methode gibt ein- DateTimeFormatInfo Objekt zurück, das Formatierungsinformationen bereitstellt.Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
Wenn formatProvider
null
den Wert hat, wird das- DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.If formatProvider
is null
, the DateTimeFormatInfo object that is associated with the current culture is used.
Der- styles
Parameter beeinflusst die Interpretation von Zeichen folgen, die mit benutzerdefinierten Format Zeichenfolgen analysiert werden.The styles
parameter affects the interpretation of strings that are parsed using custom format strings. Es bestimmt input
, ob nur als negatives Zeitintervall interpretiert wird, wenn ein negatives Vorzeichen () vorhanden ist TimeSpanStyles.None , oder ob es immer als negatives Zeitintervall () interpretiert wird TimeSpanStyles.AssumeNegative .It determines whether input
is interpreted as a negative time interval only if a negative sign is present (TimeSpanStyles.None), or whether it is always interpreted as a negative time interval (TimeSpanStyles.AssumeNegative). Wenn TimeSpanStyles.AssumeNegative nicht verwendet wird, format
muss ein literales negatives Vorzeichen (z. b. " \ -") enthalten, um ein negatives Zeitintervall erfolgreich zu analysieren.If TimeSpanStyles.AssumeNegative is not used, format
must include a literal negative sign symbol (such as "\-") to successfully parse a negative time interval.
Weitere Informationen
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. Das Format der Zeichenfolgendarstellung muss einem der angegebenen Formate genau entsprechen.The format of the string representation must match one of the specified formats exactly.
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.A string that specifies the time interval to convert.
- formats
- String[]
Ein Array standardmäßiger oder benutzerdefinierter Formatierungszeichenfolgen, das das erforderliche Format von input
definiert.An array of standard or custom format strings that defines the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von formats
und formatProvider
angegeben.A time interval that corresponds to input
, as specified by formats
and formatProvider
.
Ausnahmen
input
ist null
.input
is null
.
Das Format von input
ist ungültig.input
has an invalid format.
input
stellt eine Zahl dar, die kleiner als MinValue oder größer als MaxValue ist.input
represents a number that is less than MinValue or greater than MaxValue.
- oder --or-
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.At least one of the days, hours, minutes, or seconds components in input
is outside its valid range.
Beispiele
Im folgenden Beispiel wird die- ParseExact(String, String[], IFormatProvider) Methode aufgerufen, um die einzelnen Elemente eines Zeichen folgen Arrays in einen-Wert zu konvertieren TimeSpan .The following example calls the ParseExact(String, String[], IFormatProvider) method to convert each element of a string array to a TimeSpan value. Im Beispiel werden die Zeichen folgen mithilfe der Formatierungs Konventionen der Kultur Französisch-Frankreich ("fr-FR") interpretiert.The example interprets the strings using the formatting conventions of the French - France ("fr-FR") culture. Die Zeichen folgen können ein Zeitintervall mit dem allgemeinen Kurzformat oder dem allgemeinen langen Format darstellen.The strings can represent a time interval in either the general short format or the general long format.
Außerdem wird in diesem Beispiel die Art und Weise geändert, in der die Methoden zum Analysieren von Zeitintervallen eine einzelne Ziffer interpretieren.In addition, the example changes the way in which the time interval parsing methods interpret a single digit. Normalerweise wird eine einzelne Ziffer als Anzahl von Tagen in einem Zeitintervall interpretiert.Ordinarily, a single digit is interpreted as the number of days in a time interval. Stattdessen wird die %h
benutzerdefinierte Format Zeichenfolge verwendet, um eine einzelne Ziffer als Anzahl von Stunden zu interpretieren.Instead, the %h
custom format string is used to interpret a single digit as the number of hours. Damit diese Änderung wirksam wird, müssen Sie beachten, dass die %h
benutzerdefinierte Format Zeichenfolge den anderen Format Zeichenfolgen im Array vorangestellt ist formats
.For this change to be effective, note that the %h
custom format string must precede the other format strings in the formats
array.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] inputs = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" };
string[] formats = { "g", "G", "%h"};
TimeSpan interval;
CultureInfo culture = new CultureInfo("fr-FR");
// Parse each string in inputs using formats and the fr-FR culture.
foreach (string input in inputs) {
try {
interval = TimeSpan.ParseExact(input, formats, culture);
Console.WriteLine("{0} --> {1:c}", input, interval);
}
catch (FormatException) {
Console.WriteLine("{0} --> Bad Format", input);
}
catch (OverflowException) {
Console.WriteLine("{0} --> Overflow", input);
}
}
}
}
// The example displays the following output:
// 3 --> 03:00:00
// 16:42 --> 16:42:00
// 1:6:52:35.0625 --> Bad Format
// 1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization
Module Example
Public Sub Main()
Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625",
"1:6:52:35,0625" }
Dim formats() As String = { "%h", "g", "G" }
Dim interval As TimeSpan
Dim culture As New CultureInfo("fr-FR")
' Parse each string in inputs using formats and the fr-FR culture.
For Each input As String In inputs
Try
interval = TimeSpan.ParseExact(input, formats, culture)
Console.WriteLine("{0} --> {1:c}", input, interval)
Catch e As FormatException
Console.WriteLine("{0} --> Bad Format", input)
Catch e As OverflowException
Console.WriteLine("{0} --> Overflow", input)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 3 --> 3.00:00:00
' 16:42 --> 16:42:00
' 1:6:52:35.0625 --> Bad Format
' 1:6:52:35,0625 --> 1.06:52:35.0625000
Hinweise
Die- ParseExact(String, String, IFormatProvider) Methode analysiert die Zeichen folgen Darstellung eines Zeitintervalls, das in einem der durch den-Parameter definierten Formate vorliegen muss, mit dem Unterschied formats
, dass führende und nachfolgende Leerzeichen ignoriert werden.The ParseExact(String, String, IFormatProvider) method parses the string representation of a time interval, which must be in one of the formats defined by the formats
parameter, except that leading and trailing white-space characters are ignored. Da input
genau einem der in angegebenen Formate entsprechen muss formats
, sollten Sie immer die Ausnahmebehandlung verwenden, wenn Sie eine Zeichen folgen Eingabe vom Benutzer in ein Zeitintervall umwandelt.Because input
must exactly conform to one of the formats specified in formats
, you should always use exception handling when converting a string input by the user to a time interval. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die-Methode aufzurufen TryParseExact(String, String[], IFormatProvider, TimeSpan) .If you prefer not to use exception handling, you can call the TryParseExact(String, String[], IFormatProvider, TimeSpan) method instead.
Der- formats
Parameter ist ein Zeichen folgen Array, dessen Elemente entweder aus einem einzelnen Standardformat Bezeichner bestehen, oder aus einem oder mehreren benutzerdefinierten Format Bezeichnern, die das erforderliche Format von definieren input
.The formats
parameter is a string array whose elements consist of either a single standard format specifier, or one or more custom format specifiers that define the required format of input
. Weitere Informationen zu gültigen Format Zeichenfolgen finden Sie unter TimeSpan-Standard Format Zeichenfolgen und Benutzerdefinierte TimeSpan-FormatZeichenfolgenFor more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings. input
muss genau einem Member von entsprechen formats
, damit der Analyse Vorgang erfolgreich durchgeführt werden konnte.input
must correspond exactly to a member of formats
for the parse operation to succeed. Der Analyse Vorgang versucht, input
mit jedem Element in abzugleichen, formats
beginnend mit dem ersten Element im Array.The parse operation attempts to match input
to each element in formats
starting with the first element in the array.
Wichtig
Die- ParseExact Methode verwendet die Konventionen der Kultur, die durch den-Parameter angegeben wird formatProvider
, nur dann, wenn die zum analysieren verwendete Format Zeichenfolge input
eine Standard TimeSpan Format Zeichenfolge ist, deren Wert entweder "g" oder "g" ist.The ParseExact method uses the conventions of the culture specified by the formatProvider
parameter only if the format string used to parse input
is a standard TimeSpan format string whose value is either "g" or "G". Die Standardformat Zeichenfolgen "c", "t" und "t" verwenden die Formatierungs Konventionen der invarianten Kultur.The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Benutzerdefinierte Format Zeichenfolgen definieren das genaue Format der Eingabe Zeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls voneinander zu trennen.Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.
Der- formatProvider
Parameter ist eine- IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn die zum analysieren verwendete Format Zeichenfolge input
eine Standardformat Zeichenfolge ist.The formatProvider
parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if the format string used to parse input
is a standard format string. Der- formatProvider
Parameter kann eine der folgenden sein:The formatProvider
parameter can be any of the following:
Ein- CultureInfo Objekt, das die Kultur darstellt, deren Formatierungs Konventionen in der zurückgegebenen Zeichenfolge reflektiert werden sollen.A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. Das DateTimeFormatInfo von der-Eigenschaft zurückgegebene-Objekt CultureInfo.DateTimeFormat definiert die Formatierung der zurückgegebenen Zeichenfolge.The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
Ein- DateTimeFormatInfo Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.A DateTimeFormatInfo object that defines the formatting of the returned string.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert.A custom object that implements the IFormatProvider interface. Die IFormatProvider.GetFormat Methode gibt ein- DateTimeFormatInfo Objekt zurück, das Formatierungsinformationen bereitstellt.Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
Wenn formatProvider
null
den Wert hat, wird das- DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.If formatProvider
is null
, the DateTimeFormatInfo object that is associated with the current culture is used.
Weitere Informationen
- TryParseExact(String, String[], IFormatProvider, TimeSpan)
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
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.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly.
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.A string that specifies the time interval to convert.
- format
- String
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.A standard or custom format string that defines the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
und formatProvider
angegeben.A time interval that corresponds to input
, as specified by format
and formatProvider
.
Ausnahmen
input
ist null
.input
is null
.
Das Format von input
ist ungültig.input
has an invalid format.
input
stellt eine Zahl dar, die kleiner als MinValue oder größer als MaxValue ist.input
represents a number that is less than MinValue or greater than MaxValue.
- oder --or-
Mindestens eine der Komponenten für Tage, Stunden, Minuten oder Sekunden in input
liegt außerhalb des zulässigen Bereichs.At least one of the days, hours, minutes, or seconds components in input
is outside its valid range.
Beispiele
Im folgenden Beispiel wird die- ParseExact(String, String, IFormatProvider) Methode verwendet, um mehrere Zeichen folgen Darstellungen von Zeitintervallen mithilfe verschiedener Format Zeichenfolgen und Kulturen zu analysieren.The following example uses the ParseExact(String, String, IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture;
// Parse hour:minute value with "g" specifier current culture.
intervalString = "17:14";
format = "g";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'",
intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hour:minute:second value with "G" specifier.
intervalString = "17:14:48";
format = "G";
culture = CultureInfo.InvariantCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and current (en-US) culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = CultureInfo.CurrentCulture;
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48.153";
format = "G";
culture = new CultureInfo("fr-FR");
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse days:hours:minute.second value with "G" specifier
// and fr-FR culture.
intervalString = "3:17:14:48,153";
format = "G";
try {
interval = TimeSpan.ParseExact(intervalString, format, culture);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "c" standard format string.
intervalString = "12";
format = "c";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%h" custom format string.
format = "%h";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
// Parse a single number using the "%s" custom format string.
format = "%s";
try {
interval = TimeSpan.ParseExact(intervalString, format, null);
Console.WriteLine("'{0}' --> {1}", intervalString, interval);
}
catch (FormatException) {
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
}
catch (OverflowException) {
Console.WriteLine("'{0}': Overflow", intervalString);
}
}
}
// The example displays the following output:
// '17:14' --> 17:14:00
// '17:14:48': Bad Format for 'G'
// '17:14:48.153': Bad Format for 'G'
// '3:17:14:48.153' --> 3.17:14:48.1530000
// '3:17:14:48.153': Bad Format for 'G'
// '3:17:14:48,153' --> 3.17:14:48.1530000
// '12' --> 12.00:00:00
// '12' --> 12:00:00
// '12' --> 00:00:12
Imports System.Globalization
Module Example
Public Sub Main()
Dim intervalString, format As String
Dim interval As TimeSpan
Dim culture As CultureInfo
' Parse hour:minute value with "g" specifier current culture.
intervalString = "17:14"
format = "g"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hour:minute:second value with "G" specifier.
intervalString = "17:14:48"
format = "G"
culture = CultureInfo.InvariantCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and current (en-US) culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = CultureInfo.CurrentCulture
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48.153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse days:hours:minute.second value with "G" specifier
' and fr-FR culture.
intervalString = "3:17:14:48,153"
format = "G"
culture = New CultureInfo("fr-FR")
Try
interval = TimeSpan.ParseExact(intervalString, format, culture)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "c" standard format string.
intervalString = "12"
format = "c"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%h" custom format string.
format = "%h"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
' Parse a single number using the "%s" custom format string.
format = "%s"
Try
interval = TimeSpan.ParseExact(intervalString, format, Nothing)
Console.WriteLine("'{0}' --> {1}", intervalString, interval)
Catch e As FormatException
Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
Catch e As OverflowException
Console.WriteLine("'{0}': Overflow", intervalString)
End Try
End Sub
End Module
' The example displays the following output:
' '17:14' --> 17:14:00
' '17:14:48': Bad Format for 'G'
' '17:14:48.153': Bad Format for 'G'
' '3:17:14:48.153' --> 3.17:14:48.1530000
' '3:17:14:48.153': Bad Format for 'G'
' '3:17:14:48,153' --> 3.17:14:48.1530000
' '12' --> 12.00:00:00
' '12' --> 12:00:00
' '12' --> 00:00:12
Hinweise
Die- ParseExact(String, String, IFormatProvider) Methode analysiert die Zeichen folgen Darstellung eines Zeitintervalls, das in dem vom-Parameter definierten Format vorliegen muss, mit dem Unterschied format
, dass führende und nachfolgende Leerzeichen ignoriert werden.The ParseExact(String, String, IFormatProvider) method parses the string representation of a time interval, which must be in the format defined by the format
parameter, except that leading and trailing white-space characters are ignored. Da input
dem Format genau entsprechen muss format
, sollten Sie immer die Ausnahmebehandlung verwenden, wenn Sie eine Zeichen folgen Eingabe vom Benutzer in ein Zeitintervall umwandelt.Because input
must conform to the format of format
exactly, you should always use exception handling when converting a string input by the user to a time interval. Wenn Sie die Ausnahmebehandlung nicht verwenden möchten, können Sie stattdessen die-Methode aufzurufen TryParseExact(String, String, IFormatProvider, TimeSpan) .If you prefer not to use exception handling, you can call the TryParseExact(String, String, IFormatProvider, TimeSpan) method instead.
Der- format
Parameter ist eine Zeichenfolge, die entweder einen einzelnen Standardformat Bezeichner oder einen oder mehrere benutzerdefinierte Format Bezeichner enthält, die das erforderliche Format von definieren input
.The format
parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of input
. Weitere Informationen zu gültigen Format Zeichenfolgen finden Sie unter TimeSpan-Standard Format Zeichenfolgen und Benutzerdefinierte TimeSpan-FormatZeichenfolgenFor more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.
Wichtig
Die- ParseExact Methode verwendet die Konventionen der Kultur, die durch den- formatProvider
Parameter angegeben wird, nur, wenn format
eine Standard TimeSpan Format Zeichenfolge ist, deren Wert entweder "g" oder "g" ist.The ParseExact method uses the conventions of the culture specified by the formatProvider
parameter only if format
is a standard TimeSpan format string whose value is either "g" or "G". Die Standardformat Zeichenfolgen "c", "t" und "t" verwenden die Formatierungs Konventionen der invarianten Kultur.The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. Benutzerdefinierte Format Zeichenfolgen definieren das genaue Format der Eingabe Zeichenfolge und verwenden Literalzeichen, um die Komponenten eines Zeitintervalls voneinander zu trennen.Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.
Der- formatProvider
Parameter ist eine- IFormatProvider Implementierung, die kulturspezifische Informationen zum Format der zurückgegebenen Zeichenfolge bereitstellt, wenn format
eine Standardformat Zeichenfolge ist.The formatProvider
parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if format
is a standard format string. Der- formatProvider
Parameter kann eine der folgenden sein:The formatProvider
parameter can be any of the following:
Ein- CultureInfo Objekt, das die Kultur darstellt, deren Formatierungs Konventionen in der zurückgegebenen Zeichenfolge reflektiert werden sollen.A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. Das DateTimeFormatInfo von der-Eigenschaft zurückgegebene-Objekt CultureInfo.DateTimeFormat definiert die Formatierung der zurückgegebenen Zeichenfolge.The DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
Ein- DateTimeFormatInfo Objekt, das die Formatierung der zurückgegebenen Zeichenfolge definiert.A DateTimeFormatInfo object that defines the formatting of the returned string.
Ein benutzerdefiniertes Objekt, das die IFormatProvider Schnittstelle implementiert.A custom object that implements the IFormatProvider interface. Die IFormatProvider.GetFormat Methode gibt ein- DateTimeFormatInfo Objekt zurück, das Formatierungsinformationen bereitstellt.Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
Wenn formatProvider
null
den Wert hat, wird das- DateTimeFormatInfo Objekt verwendet, das der aktuellen Kultur zugeordnet ist.If formatProvider
is null
, the DateTimeFormatInfo object that is associated with the current culture is used.
Weitere Informationen
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
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.Converts the char span of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Das Format der Zeichenfolgendarstellung muss dem angegebenen Format genau entsprechen.The format of the string representation must match the specified format exactly.
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.A span that specifies the time interval to convert.
- format
- ReadOnlySpan<Char>
Eine standardmäßige oder benutzerdefinierte Formatierungszeichenfolge, die das erforderliche Format von input
definiert.A standard or custom format string that defines the required format of input
.
- formatProvider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that provides culture-specific formatting information.
- styles
- TimeSpanStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente definiert, die in der input
vorhanden sein können.A bitwise combination of enumeration values that defines the style elements that may be present in input
.
Gibt zurück
Ein Zeitintervall, das input
entspricht, wie von format
und formatProvider
angegeben.A time interval that corresponds to input
, as specified by format
and formatProvider
.