TimeSpan.ParseExact Metoda

Definicja

Konwertuje reprezentację ciągu interwału czasu na jego TimeSpan odpowiednik. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

Przeciążenia

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

Konwertuje reprezentację ciągu interwału czasu na TimeSpan odpowiednik przy użyciu określonego formatu, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)

Konwertuje reprezentację ciągu interwału czasu na TimeSpan odpowiednik przy użyciu określonych formatów, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

ParseExact(String, String[], IFormatProvider, TimeSpanStyles)

Konwertuje reprezentację ciągu interwału czasu na TimeSpan odpowiednik przy użyciu określonych formatów, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

ParseExact(String, String[], IFormatProvider)

Konwertuje reprezentację ciągu interwału czasu na jego TimeSpan odpowiednik przy użyciu określonej tablicy ciągów formatu i informacji o formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

ParseExact(String, String, IFormatProvider)

Konwertuje reprezentację ciągu interwału czasu na jego TimeSpan odpowiednik przy użyciu określonych formatów i informacji o formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

Konwertuje zakres znaków interwału czasu na jego TimeSpan odpowiednik przy użyciu informacji o określonym formacie i formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje reprezentację ciągu interwału czasu na TimeSpan odpowiednik przy użyciu określonego formatu, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

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

Parametry

input
String

Ciąg określający przedział czasu, którą należy przekształcić.

format
String

Ciąg formatu standardowego lub niestandardowego input, który definiuje wymagany format .

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

styles
TimeSpanStyles

Bitowa kombinacja wartości wyliczenia definiujących elementy stylu, które mogą być obecne w inputobiekcie .

Zwraca

Interwał czasu odpowiadający parametrowi input, określony przez format, formatProvideri styles.

Wyjątki

styles jest nieprawidłową TimeSpanStyles wartością.

input to null.

input ma nieprawidłowy format.

input reprezentuje liczbę, która jest mniejsza niż TimeSpan.MinValue lub większa niż TimeSpan.MaxValue.

-lub-

Co najmniej jeden z dni, godzin, minut lub sekund składników znajduje input się poza prawidłowym zakresem.

Przykłady

W poniższym przykładzie ParseExact(String, String, IFormatProvider) użyto metody do analizowania kilku reprezentacji ciągów interwałów czasu przy użyciu różnych ciągów formatu i kultur. Używa TimeSpanStyles.AssumeNegative również wartości do interpretowania każdego ciągu jako ujemnego interwału czasu. Dane wyjściowe z przykładu ilustrują, że TimeSpanStyles.AssumeNegative styl wpływa na wartość zwracaną tylko wtedy, gdy jest używany z ciągami formatu niestandardowego.

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

Uwagi

Metoda ParseExact analizuje reprezentację ciągu interwału czasu, który musi być w formacie zdefiniowanym przez format parametr, z tą różnicą, że znaki wiodące i końcowe są ignorowane. Ponieważ input musi być zgodny z formatem dokładnie, należy zawsze używać obsługi wyjątków format podczas konwertowania ciągu wejściowego przez użytkownika na interwał czasu. Jeśli wolisz nie używać obsługi wyjątków, możesz zamiast tego wywołać metodę TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) .

Parametr format jest ciągiem zawierającym jeden specyfikator formatu standardowego lub co najmniej jeden specyfikator formatu niestandardowego inputdefiniujący wymagany format . Aby uzyskać więcej informacji na temat prawidłowych ciągów formatu, zobacz Ciągi formatu Czas standardowy i Niestandardowe ciągi formatu TimeSpan.

Ważne

Metoda ParseExact używa konwencji kultury określonej przez formatProvider parametr tylko wtedy, gdy format jest standardowym TimeSpan ciągiem formatu, którego wartość to "g" lub "G". Standardowe ciągi formatujące „c”, „t” i „T” wykorzystują konwencje formatowania niezmiennej kultury. Niestandardowe ciągi formatujące definiują dokładny format ciągu wejściowego oraz za pomocą znaków literałowych oddzielają składniki przedziału czasu.

Parametr formatProvider jest implementacją IFormatProvider , która udostępnia informacje specyficzne dla kultury dotyczące formatu zwracanego ciągu, jeśli format jest standardowym ciągiem formatu. Parametr formatProvider może być dowolny z następujących:

Jeśli formatProvider jest to null, DateTimeFormatInfo używany jest obiekt skojarzony z bieżącą kulturą.

Parametr styles ma wpływ na interpretację ciągów, które są analizowane przy użyciu ciągów formatu niestandardowego. Określa, czy input jest interpretowany jako ujemny interwał czasu tylko wtedy, gdy znak ujemny jest obecny (TimeSpanStyles.None), czy zawsze jest interpretowany jako ujemny interwał czasu (TimeSpanStyles.AssumeNegative). Jeśli TimeSpanStyles.AssumeNegative nie jest używany, format musi zawierać symbol znaku ujemnego literału (np. "\-"), aby pomyślnie przeanalizować ujemny interwał czasu.

Zobacz też

Dotyczy

ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje reprezentację ciągu interwału czasu na TimeSpan odpowiednik przy użyciu określonych formatów, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

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

Parametry

input
ReadOnlySpan<Char>

Zakres określający interwał czasu do przekonwertowania.

formats
String[]

Tablica ciągów formatu standardowego lub niestandardowego, które definiują wymagany format input.

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

styles
TimeSpanStyles

Bitowa kombinacja wartości wyliczenia definiujących elementy stylu, które mogą być obecne w danych wejściowych.

Zwraca

Interwał czasu odpowiadający parametrowi input, określony przez formats, formatProvideri styles.

Dotyczy

ParseExact(String, String[], IFormatProvider, TimeSpanStyles)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje reprezentację ciągu interwału czasu na jego TimeSpan odpowiednik przy użyciu określonych formatów, informacji o formacie specyficznym dla kultury i stylów. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

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

Parametry

input
String

Ciąg określający przedział czasu, którą należy przekształcić.

formats
String[]

Tablica ciągów formatu standardowego lub niestandardowego, które definiują wymagany format input.

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

styles
TimeSpanStyles

Bitowa kombinacja wartości wyliczenia definiujących elementy stylu, które mogą być obecne w danych wejściowych.

Zwraca

Interwał czasu odpowiadający inputfunkcji , określony przez formats, formatProvideri styles.

Wyjątki

styles jest nieprawidłową TimeSpanStyles wartością.

input to null.

input ma nieprawidłowy format.

input reprezentuje liczbę, która jest mniejsza niż TimeSpan.MinValue lub większa niż TimeSpan.MaxValue.

-lub-

Co najmniej jeden z dni, godzin, minut lub sekund składników znajduje input się poza prawidłowym zakresem.

Przykłady

Poniższy przykład wywołuje metodę ParseExact(String, String[], IFormatProvider, TimeSpanStyles) , aby przekonwertować każdy element tablicy ciągów na TimeSpan wartość. Ciągi mogą reprezentować przedział czasu w ogólnym formacie krótkim lub długim.

Ponadto w przykładzie zmienił się sposób, w jaki metody analizy przedziałów czasu interpretują pojedyncze cyfry. Zazwyczaj w odstępie czasu pojedyncza cyfra jest interpretowana jako liczba dni. %h Zamiast tego ciąg formatu niestandardowego służy do interpretowania pojedynczej cyfry jako liczby godzin. Aby ta zmiana obowiązywała, należy pamiętać, że %h ciąg formatu niestandardowego musi poprzedzać inne ciągi formatu w tablicy formats . Zwróć również uwagę na dane wyjściowe, że TimeSpanStyles.AssumeNegative flaga określona w wywołaniu metody jest używana tylko w przypadku analizowania ciągu za pomocą tego specyfikatora formatu.

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

Uwagi

Metoda ParseExact(String, String[], IFormatProvider, TimeSpanStyles) analizuje reprezentację ciągu interwału czasu, który musi znajdować się w jednym z formatów zdefiniowanych przez formats parametr, z wyjątkiem tego, że znaki wiodące i końcowe białych znaków są ignorowane. Ponieważ input musi być dokładnie zgodny z jednym z formatów określonych w formatsprogramie , należy zawsze używać obsługi wyjątków podczas konwertowania ciągu wejściowego przez użytkownika na interwał czasu. Jeśli wolisz nie używać obsługi wyjątków, możesz zamiast tego wywołać metodę TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) .

Parametr formats jest tablicą ciągów, której elementy składają się z jednego standardowego specyfikatora formatu lub co najmniej jednego specyfikatora formatu niestandardowego, które definiują wymagany format input. Aby uzyskać więcej informacji na temat prawidłowych ciągów formatu, zobacz Ciągi formatu Czas standardowy i Niestandardowe ciągi formatu TimeSpan. input musi odpowiadać dokładnie elementowi członkowskiemu formats operacji analizowania, aby operacja analizy zakończyła się pomyślnie. Operacja analizy próbuje dopasować input do każdego elementu, formats zaczynając od pierwszego elementu w tablicy.

Ważne

Metoda ParseExact używa konwencji kultury określonej przez formatProvider parametr tylko wtedy, gdy ciąg formatu używany do analizowania input jest standardowym TimeSpan ciągiem formatu, którego wartość to "g" lub "G". Standardowe ciągi formatujące „c”, „t” i „T” wykorzystują konwencje formatowania niezmiennej kultury. Niestandardowe ciągi formatujące definiują dokładny format ciągu wejściowego oraz za pomocą znaków literałowych oddzielają składniki przedziału czasu.

Parametr formatProvider jest implementacją IFormatProvider , która dostarcza informacje specyficzne dla kultury dotyczące formatu zwracanego ciągu, jeśli ciąg formatu używany do analizy input jest standardowym ciągiem formatu. Parametr formatProvider może być dowolnym z następujących:

Jeśli formatProvider parametr ma nullwartość , DateTimeFormatInfo używany jest obiekt skojarzony z bieżącą kulturą.

Parametr styles ma wpływ na interpretację ciągów, które są analizowane przy użyciu ciągów formatu niestandardowego. Określa, czy input jest interpretowany jako ujemny interwał czasu tylko wtedy, gdy znak ujemny jest obecny (TimeSpanStyles.None) lub czy jest zawsze interpretowany jako ujemny interwał czasu (TimeSpanStyles.AssumeNegative). Jeśli TimeSpanStyles.AssumeNegative nie jest używany, format musi zawierać symbol znaku ujemnego literału (np. "\-"), aby pomyślnie przeanalizować ujemny interwał czasu.

Zobacz też

Dotyczy

ParseExact(String, String[], IFormatProvider)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje reprezentację ciągu interwału czasu na jego TimeSpan odpowiednik przy użyciu określonej tablicy ciągów formatu i informacji o formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do jednego ze wskazanych formatów.

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

Parametry

input
String

Ciąg określający przedział czasu, którą należy przekształcić.

formats
String[]

Tablica ciągów formatu standardowego lub niestandardowego, która definiuje wymagany format input.

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

Zwraca

Przedział czasu, który odpowiada parametrowi input, zgodnie z parametrami formats i formatProvider.

Wyjątki

input to null.

input ma nieprawidłowy format.

input reprezentuje liczbę mniejszą niż TimeSpan.MinValue lub większą niż TimeSpan.MaxValue.

-lub-

Co najmniej jeden z dni, godzin, minut lub sekund w składniku input znajduje się poza prawidłowym zakresem.

Przykłady

Poniższy przykład wywołuje metodę , ParseExact(String, String[], IFormatProvider) aby przekonwertować każdy element tablicy ciągów na TimeSpan wartość. Przykład interpretuje ciągi przy użyciu konwencji formatowania kultury Francuski - Francja ("fr-FR"). Ciągi mogą reprezentować przedział czasu w ogólnym formacie krótkim lub długim.

Ponadto w przykładzie zmienił się sposób, w jaki metody analizy przedziałów czasu interpretują pojedyncze cyfry. Zazwyczaj w odstępie czasu pojedyncza cyfra jest interpretowana jako liczba dni. Zamiast tego ciąg formatu niestandardowego %h służy do interpretowania pojedynczej cyfry jako liczby godzin. Aby ta zmiana obowiązywała, należy pamiętać, że %h ciąg formatu niestandardowego musi poprzedzać inne ciągi formatu w tablicy formats .

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "g", "G", "%h"};
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
open System
open System.Globalization

let inputs = [| "3"; "16:42"; "1:6:52:35.0625"; "1:6:52:35,0625" |] 
let formats = [| "g"; "G"; "%h" |]
let culture = CultureInfo "fr-FR"

// Parse each string in inputs using formats and the fr-FR culture.
for input in inputs do
    try
        let interval = TimeSpan.ParseExact(input, formats, culture)
        printfn $"{input} --> {interval:c}"
    with
    | :? FormatException ->
        printfn $"{input} --> Bad Format"
    | :? OverflowException ->
        printfn $"{input} --> Overflow"
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625", 
                                 "1:6:52:35,0625" } 
      Dim formats() As String = { "%h", "g", "G" }
      Dim interval As TimeSpan
      Dim culture As New CultureInfo("fr-FR")
      
      ' Parse each string in inputs using formats and the fr-FR culture.
      For Each input As String In inputs
         Try
            interval = TimeSpan.ParseExact(input, formats, culture)
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Catch e As FormatException
            Console.WriteLine("{0} --> Bad Format", input)   
         Catch e As OverflowException
            Console.WriteLine("{0} --> Overflow", input)   
         End Try            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> 3.00:00:00
'       16:42 --> 16:42:00
'       1:6:52:35.0625 --> Bad Format
'       1:6:52:35,0625 --> 1.06:52:35.0625000

Uwagi

Metoda ParseExact(String, String, IFormatProvider) analizuje reprezentację ciągu interwału czasu, który musi być w jednym z formatów zdefiniowanych przez formats parametr, z tą różnicą, że znaki wiodące i końcowe białych znaków są ignorowane. Ponieważ input musi być dokładnie zgodny z jednym z formatów określonych w formatsprogramie , należy zawsze używać obsługi wyjątków podczas konwertowania ciągu wejściowego przez użytkownika na przedział czasu. Jeśli nie chcesz używać obsługi wyjątków, możesz wywołać metodę TryParseExact(String, String[], IFormatProvider, TimeSpan) .

Parametr formats jest tablicą ciągów, której elementy składają się z jednego specyfikatora formatu standardowego lub co najmniej jednego specyfikatora formatu niestandardowego, które definiują wymagany format input. Aby uzyskać więcej informacji na temat prawidłowych ciągów formatu, zobacz Ciągi formatu Standard TimeSpan i Niestandardowe ciągi formatu TimeSpan. input musi odpowiadać dokładnie członkowi formats operacji analizy, aby operacja analizy powiodła się. Operacja analizy próbuje dopasować input do każdego elementu, formats zaczynając od pierwszego elementu w tablicy.

Ważne

Metoda ParseExact używa konwencji kultury określonej przez formatProvider parametr tylko wtedy, gdy ciąg formatu używany do analizowania input jest standardowym TimeSpan ciągiem formatu, którego wartość to "g" lub "G". Standardowe ciągi formatujące „c”, „t” i „T” wykorzystują konwencje formatowania niezmiennej kultury. Niestandardowe ciągi formatujące definiują dokładny format ciągu wejściowego oraz za pomocą znaków literałowych oddzielają składniki przedziału czasu.

Parametr formatProvider jest implementacją IFormatProvider , która dostarcza informacje specyficzne dla kultury dotyczące formatu zwracanego ciągu, jeśli ciąg formatu używany do analizy input jest standardowym ciągiem formatu. Parametr formatProvider może być dowolnym z następujących:

Jeśli formatProvider parametr ma nullwartość , DateTimeFormatInfo używany jest obiekt skojarzony z bieżącą kulturą.

Zobacz też

Dotyczy

ParseExact(String, String, IFormatProvider)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje reprezentację ciągu przedziału czasu na jego TimeSpan odpowiednik przy użyciu określonego formatu i informacji o formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

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

Parametry

input
String

Ciąg określający przedział czasu, którą należy przekształcić.

format
String

Ciąg formatu standardowego lub niestandardowego, który definiuje wymagany format input.

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

Zwraca

Przedział czasu, który odpowiada parametrowi input, zgodnie z parametrami format i formatProvider.

Wyjątki

input to null.

input ma nieprawidłowy format.

input reprezentuje liczbę mniejszą niż TimeSpan.MinValue lub większą niż TimeSpan.MaxValue.

-lub-

Co najmniej jeden z dni, godzin, minut lub sekund w składniku input znajduje się poza prawidłowym zakresem.

Przykłady

W poniższym przykładzie użyto ParseExact(String, String, IFormatProvider) metody , aby przeanalizować kilka reprezentacji ciągów interwałów czasu przy użyciu różnych ciągów formatu i kultur.

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

Uwagi

Metoda ParseExact(String, String, IFormatProvider) analizuje reprezentację ciągu interwału czasu, który musi być w formacie zdefiniowanym przez format parametr, z tą różnicą, że znaki wiodące i końcowe odstępów są ignorowane. Ponieważ input musi być dokładnie zgodny z format formatem, należy zawsze używać obsługi wyjątków podczas konwertowania ciągu wejściowego przez użytkownika na przedział czasu. Jeśli nie chcesz używać obsługi wyjątków, możesz wywołać metodę TryParseExact(String, String, IFormatProvider, TimeSpan) .

Parametr format jest ciągiem zawierającym jeden specyfikator formatu standardowego lub co najmniej jeden specyfikator formatu niestandardowego inputdefiniujący wymagany format . Aby uzyskać więcej informacji na temat prawidłowych ciągów formatu, zobacz Ciągi formatu Standard TimeSpan i Niestandardowe ciągi formatu TimeSpan.

Ważne

Metoda ParseExact używa konwencji kultury określonej przez formatProvider parametr tylko wtedy, gdy format jest standardowym TimeSpan ciągiem formatu, którego wartość to "g" lub "G". Standardowe ciągi formatujące „c”, „t” i „T” wykorzystują konwencje formatowania niezmiennej kultury. Niestandardowe ciągi formatujące definiują dokładny format ciągu wejściowego oraz za pomocą znaków literałowych oddzielają składniki przedziału czasu.

Parametr formatProvider jest implementacją IFormatProvider , która dostarcza informacje specyficzne dla kultury dotyczące formatu zwracanego ciągu, jeśli format jest standardowym ciągiem formatu. Parametr formatProvider może być dowolnym z następujących:

Jeśli formatProvider parametr ma nullwartość , DateTimeFormatInfo używany jest obiekt skojarzony z bieżącą kulturą.

Zobacz też

Dotyczy

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs
Źródło:
TimeSpan.cs

Konwertuje przedział czasu przedziału czasu na jego TimeSpan odpowiednik przy użyciu określonego formatu i informacji o formacie specyficznym dla kultury. Format ciągu reprezentującego musi dokładnie pasować do wskazanego formatu.

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

Parametry

input
ReadOnlySpan<Char>

Zakres określający interwał czasu, który ma być konwertowany.

format
ReadOnlySpan<Char>

Ciąg formatu standardowego lub niestandardowego, który definiuje wymagany format input.

formatProvider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury.

styles
TimeSpanStyles

Bitowa kombinacja wartości wyliczenia, która definiuje elementy stylu, które mogą być obecne w .input

Zwraca

Przedział czasu, który odpowiada parametrowi input, zgodnie z parametrami format i formatProvider.

Dotyczy