TimeSpan.TryParseExact Metode

Definisi

Mengonversi representasi string interval waktu menjadi TimeSpan setara, dan mengembalikan nilai yang menunjukkan apakah konversi berhasil. Format representasi string harus sama persis dengan format yang ditentukan.

Overload

TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)

Mengonversi representasi string dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

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

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles, TimeSpan)

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi format khusus budaya, dan gaya, dan mengembalikan nilai yang menunjukkan apakah konversi berhasil. Format representasi string harus sama persis dengan format yang ditentukan.

TryParseExact(String, String[], IFormatProvider, TimeSpan)

Mengonversi representasi string yang ditentukan dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

Mengonversi representasi string yang ditentukan dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpan)

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpan)

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

TryParseExact(String, String, IFormatProvider, TimeSpan)

Mengonversi representasi string interval waktu menjadi TimeSpan setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi string dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (string? input, string? format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (string? input, string format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : string * string * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

Parameter

input
String

String yang menentukan interval waktu untuk dikonversi.

format
String

String format standar atau kustom yang menentukan format yang diperlukan dari input.

formatProvider
IFormatProvider

Objek yang menyediakan informasi pemformatan khusus budaya.

styles
TimeSpanStyles

Satu atau beberapa nilai enumerasi yang menunjukkan gaya input.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Contoh

Contoh berikut menggunakan ParseExact(String, String, IFormatProvider) metode untuk mengurai beberapa representasi string interval waktu menggunakan berbagai string format dan budaya. Ini juga menggunakan TimeSpanStyles.AssumeNegative nilai untuk menginterpretasikan setiap string sebagai interval waktu negatif. Output dari contoh menggambarkan bahwa TimeSpanStyles.AssumeNegative gaya hanya memengaruhi nilai yang dikembalikan saat digunakan dengan string format kustom.

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;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48";
      format = "g";
      culture = CultureInfo.InvariantCulture;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153";
      format = @"h\:mm\:ss\.fff";
      culture = null;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);   

      // 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;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);   
            
      // 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;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // 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");
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
   }
}
// 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
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 
    
    // Parse hour:minute:second value with "g" specifier.
    let intervalString = "17:14:48"
    let format = "g"
    let culture = CultureInfo.InvariantCulture
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->
        printfn $"Unable to parse '{intervalString}' using format {format}" 
    
    // Parse hours:minute.second value with custom format specifier.     
    let intervalString = "17:14:48.153"
    let format = @"h\:mm\:ss\.fff"
    let culture = null
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->
        printfn $"Unable to parse '{intervalString}' using format {format}"    

    // 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
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}"    
        
    // 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
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 
    
    // 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")
    match TimeSpan.TryParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    match TimeSpan.TryParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    match TimeSpan.TryParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    match TimeSpan.TryParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"'{intervalString}' ({format}) --> {interval}"
    | _ ->   
        printfn $"Unable to parse '{intervalString}' using format {format}" 
// 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
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If
      
      ' Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48"
      format = "g"
      culture = CultureInfo.InvariantCulture
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If
      
      ' Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153"
      format = "h\:mm\:ss\.fff"
      culture = Nothing
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 

      ' 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
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
            
      ' 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
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' 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")
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)
      End If 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
   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

Keterangan

Metode ini TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) menguraikan representasi string dari interval waktu, yang harus dalam format yang ditentukan oleh format parameter, kecuali bahwa karakter spasi putih di awal dan akhir diabaikan. Metode ini mirip ParseExact(String, String, IFormatProvider, TimeSpanStyles) dengan metode , kecuali bahwa metode ini tidak memberikan pengecualian jika konversi gagal.

Parameter format adalah string yang berisi penentu format standar tunggal, atau satu atau beberapa penentu format kustom yang menentukan format yang diperlukan dari input. Untuk informasi selengkapnya tentang string format yang valid, lihat String Format Rentang Waktu Standar dan String Format Rentang Waktu Kustom.

Parameter formatProvider adalah IFormatProvider implementasi yang menyediakan informasi khusus budaya tentang format string yang dikembalikan jika format merupakan string format standar. Parameter formatProvider dapat berupa salah satu hal berikut:

Jika formatProvider adalah null, DateTimeFormatInfo objek yang terkait dengan budaya saat ini digunakan.

Parameter styles memengaruhi interpretasi string yang diurai menggunakan string format kustom. Ini menentukan apakah input ditafsirkan sebagai interval waktu negatif hanya jika tanda negatif ada (TimeSpanStyles.None), atau apakah itu selalu ditafsirkan sebagai interval waktu negatif (TimeSpanStyles.AssumeNegative). Jika TimeSpanStyles.AssumeNegative tidak digunakan, format harus menyertakan simbol tanda negatif harfiah (seperti "\-") agar berhasil mengurai interval waktu negatif.

Lihat juga

Berlaku untuk

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

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

Parameter

input
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili interval waktu untuk dikonversi.

formats
String[]

Array string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

styles
TimeSpanStyles

Satu atau beberapa nilai enumerasi yang menunjukkan gaya input.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Berlaku untuk

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi format khusus budaya, dan gaya, dan mengembalikan nilai yang menunjukkan apakah konversi berhasil. Format representasi string harus sama persis dengan format yang ditentukan.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

Parameter

input
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili interval waktu untuk dikonversi.

format
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

styles
TimeSpanStyles

Satu atau beberapa nilai enumerasi yang menunjukkan gaya input.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Berlaku untuk

TryParseExact(String, String[], IFormatProvider, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi string yang ditentukan dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParseExact (string? input, string[] formats, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParseExact : string * string[] * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

Parameter

input
String

String yang menentukan interval waktu untuk dikonversi.

formats
String[]

Array string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang menyediakan informasi pemformatan khusus budaya.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Contoh

Contoh berikut memanggil TryParseExact(String, String[], IFormatProvider, TimeSpan) metode untuk mengonversi setiap elemen array string menjadi TimeSpan nilai. Contoh ini menafsirkan string dengan menggunakan konvensi pemformatan budaya Prancis - Prancis ("fr-FR"). String dapat mewakili interval waktu dalam format pendek umum atau format panjang umum.

Selain itu, contoh mengubah cara metode penguraian interval waktu menginterpretasikan satu digit. Biasanya, satu digit ditafsirkan sebagai jumlah hari dalam interval waktu. Sebagai gantinya %h , string format kustom digunakan untuk menginterpretasikan satu digit sebagai jumlah jam. Agar perubahan ini efektif, perhatikan bahwa %h string format kustom harus mendahului string format lainnya dalam 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) {
         if(TimeSpan.TryParseExact(input, formats, culture, out interval))
            Console.WriteLine("{0} --> {1:c}", input, interval);
         else
            Console.WriteLine("Unable to parse {0}", input);   
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       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
    match TimeSpan.TryParseExact(input, formats, culture) with
    | true, interval ->
        printfn $"{input} --> {interval:c}"
    | _ ->
        printfn $"Unable to parse {input}"
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       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
         If TimeSpan.TryParseExact(input, formats, culture, interval) Then
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Else
            Console.WriteLine("Unable to parse {0}", input)   
         End If            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> 03:00:00
'       16:42 --> 16:42:00
'       Unable to parse 1:6:52:35.0625
'       1:6:52:35,0625 --> 1.06:52:35.0625000

Keterangan

Metode ini TryParseExact(String, String[], IFormatProvider, TimeSpan) menguraikan representasi string dari interval waktu, yang harus dalam format yang ditentukan oleh salah satu string format yang ditentukan oleh formats parameter, kecuali bahwa karakter spasi putih di depan dan di belakang diabaikan. Metode ini mirip ParseExact(String, String[], IFormatProvider) dengan metode , kecuali bahwa metode ini tidak memberikan pengecualian jika konversi gagal.

Parameter formats adalah array string yang elemennya terdiri dari penentu format standar tunggal, atau satu atau beberapa penentu format kustom yang menentukan format yang diperlukan dari input. Untuk informasi selengkapnya tentang string format yang valid, lihat String Format Rentang Waktu Standar dan String Format Rentang Waktu Kustom. input harus sesuai persis dengan anggota agar operasi penguraian formats berhasil. Operasi penguraian mencoba mencocokkan input dengan setiap elemen dalam formats dimulai dengan elemen pertama dalam array.

Penting

Metode ini TryParseExact menggunakan konvensi budaya yang ditentukan oleh formatProvider parameter hanya jika string format yang digunakan untuk mengurai input adalah string format standar TimeSpan yang nilainya adalah "g" atau "G". String format standar "c", "t", dan "T" menggunakan konvensi pemformatan budaya invarian. String format kustom menentukan format yang tepat dari string input dan menggunakan karakter literal untuk memisahkan komponen interval waktu.

Parameter formatProvider adalah IFormatProvider implementasi yang menyediakan informasi khusus budaya tentang format string yang dikembalikan jika string format yang digunakan untuk mengurai input adalah string format standar. Parameter formatProvider dapat berupa salah satu hal berikut:

Jika formatProvider adalah null, DateTimeFormatInfo objek yang terkait dengan budaya saat ini digunakan.

Lihat juga

Berlaku untuk

TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi string yang ditentukan dari interval waktu ke TimeSpan yang setara dengan menggunakan format yang ditentukan, informasi dan gaya format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (string? input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

Parameter

input
String

String yang menentukan interval waktu untuk dikonversi.

formats
String[]

Array string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

styles
TimeSpanStyles

Satu atau beberapa nilai enumerasi yang menunjukkan gaya input.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Contoh

Contoh berikut memanggil TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) metode untuk mengonversi setiap elemen array string menjadi TimeSpan nilai. String dapat mewakili interval waktu dalam format pendek umum atau format panjang umum.

Selain itu, contoh mengubah cara metode penguraian interval waktu menginterpretasikan satu digit. Biasanya, satu digit ditafsirkan sebagai jumlah hari dalam interval waktu. Sebagai gantinya %h , string format kustom digunakan untuk menginterpretasikan satu digit sebagai jumlah jam. Agar perubahan ini efektif, perhatikan bahwa %h string format kustom harus mendahului string format lainnya dalam formats array. Perhatikan juga dari output bahwa bendera yang TimeSpanStyles.AssumeNegative ditentukan dalam panggilan metode hanya digunakan saat mengurai string dengan penentu format ini.

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("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         if(TimeSpan.TryParseExact(input, formats, culture, 
                                   TimeSpanStyles.AssumeNegative, out interval))
            Console.WriteLine("{0} --> {1:c}", input, interval);
         else
            Console.WriteLine("Unable to parse {0}", input);   
      }
   }
}
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       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 "fr-FR"

// Parse each string in inputs using formats and the fr-FR culture.
for input in inputs do
    match TimeSpan.TryParseExact(input, formats, culture, TimeSpanStyles.AssumeNegative) with
    | true, interval ->
        printfn $"{input} --> {interval:c}"
    | _ ->
        printfn $"Unable to parse {input}"
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       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 fr-FR culture.
      For Each input As String In inputs
         If TimeSpan.TryParseExact(input, formats, culture, 
                                   TimeSpanStyles.AssumeNegative, interval) Then
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Else
            Console.WriteLine("Unable to parse {0}", input)   
         End If            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> -03:00:00
'       16:42 --> 16:42:00
'       Unable to parse 1:6:52:35.0625
'       1:6:52:35,0625 --> 1.06:52:35.0625000

Keterangan

Metode ini TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) menguraikan representasi string dari interval waktu, yang harus dalam format yang ditentukan oleh salah satu string format yang ditentukan oleh formats parameter, kecuali bahwa karakter spasi putih di depan dan di belakang diabaikan. Metode ini mirip ParseExact(String, String[], IFormatProvider, TimeSpanStyles) dengan metode , kecuali bahwa metode ini tidak memberikan pengecualian jika konversi gagal.

Parameter formats adalah array string yang elemennya terdiri dari penentu format standar tunggal, atau satu atau beberapa penentu format kustom yang menentukan format yang diperlukan dari input. Untuk informasi selengkapnya tentang string format yang valid, lihat String Format Rentang Waktu Standar dan String Format Rentang Waktu Kustom. input harus sesuai persis dengan anggota agar operasi penguraian formats berhasil. Operasi penguraian mencoba mencocokkan input dengan setiap elemen dalam formats dimulai dengan elemen pertama dalam array.

Penting

Metode ini ParseExact menggunakan konvensi budaya yang ditentukan oleh formatProvider parameter hanya jika string format yang digunakan untuk mengurai input adalah string format standar TimeSpan yang nilainya adalah "g" atau "G". String format standar "c", "t", dan "T" menggunakan konvensi pemformatan budaya invarian. String format kustom menentukan format yang tepat dari string input dan menggunakan karakter literal untuk memisahkan komponen interval waktu.

Parameter formatProvider adalah IFormatProvider implementasi yang menyediakan informasi khusus budaya tentang format string yang dikembalikan jika string format yang digunakan untuk mengurai input adalah string format standar. Parameter formatProvider dapat berupa salah satu hal berikut:

Jika formatProvider adalah null, DateTimeFormatInfo objek yang terkait dengan budaya saat ini digunakan.

Parameter styles memengaruhi interpretasi string yang diurai menggunakan string format kustom. Ini menentukan apakah input ditafsirkan sebagai interval waktu negatif hanya jika tanda negatif ada (TimeSpanStyles.None), atau apakah itu selalu ditafsirkan sebagai interval waktu negatif (TimeSpanStyles.AssumeNegative). Jika TimeSpanStyles.AssumeNegative tidak digunakan, format harus menyertakan simbol tanda negatif harfiah (seperti "\-") agar berhasil mengurai interval waktu negatif.

Lihat juga

Berlaku untuk

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan salah satu format yang ditentukan.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

Parameter

input
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili interval waktu untuk dikonversi.

formats
String[]

Array string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Berlaku untuk

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi rentang interval waktu yang ditentukan ke TimeSpan yang setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

Parameter

input
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili interval waktu untuk dikonversi.

format
ReadOnlySpan<Char>

Rentang yang berisi karakter yang mewakili string format standar atau kustom yang menentukan format yang dapat diterima dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Berlaku untuk

TryParseExact(String, String, IFormatProvider, TimeSpan)

Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs
Sumber:
TimeSpan.cs

Mengonversi representasi string interval waktu menjadi TimeSpan setara dengan menggunakan format yang ditentukan dan informasi format khusus budaya. Format representasi string harus sama persis dengan format yang ditentukan.

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParseExact (string? input, string? format, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParseExact (string? input, string format, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParseExact : string * string * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As String, format As String, formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

Parameter

input
String

String yang menentukan interval waktu untuk dikonversi.

format
String

String format standar atau kustom yang menentukan format yang diperlukan dari input.

formatProvider
IFormatProvider

Objek yang memasok informasi pemformatan khusus budaya.

result
TimeSpan

Ketika metode ini kembali, berisi objek yang mewakili interval waktu yang ditentukan oleh input, atau Zero jika konversi gagal. Parameter ini diteruskan tanpa diinisialisasi.

Mengembalikan

true jika input berhasil dikonversi; jika tidak, false.

Contoh

Contoh berikut menggunakan TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) metode untuk mengurai beberapa representasi string interval waktu menggunakan berbagai string format dan budaya.

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;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", 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;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", 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");
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       Unable to parse 17:14:48
//       Unable to parse 17:14:48.153
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       Unable to parse 3:17:14:48.153
//       '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
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
    
    // Parse hour:minute:second value with "G" specifier.
    let intervalString = "17:14:48"
    let format = "G"
    let culture = CultureInfo.InvariantCulture
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
    
    // 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
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"

    // 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
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
        
    // 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")
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
    
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48,153"
    let format = "G"
    match TimeSpan.TryParseExact(intervalString, format, culture) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    match TimeSpan.TryParseExact(intervalString, format, null) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    match TimeSpan.TryParseExact(intervalString, format, null) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    match TimeSpan.TryParseExact(intervalString, format, null) with
    | true, interval ->
        printfn $"'{intervalString}' --> {interval}"
    | _ ->
        printfn $"Unable to parse {intervalString}"
// The example displays the following output:
//       '17:14' --> 17:14:00
//       Unable to parse 17:14:48
//       Unable to parse 17:14:48.153
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       Unable to parse 3:17:14:48.153
//       '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
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48"
      format = "G"
      culture = CultureInfo.InvariantCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' Parse hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' 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
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
            
      ' 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")
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' 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")
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
   End Sub
End Module
' The example displays the following output:
'       '17:14' --> 17:14:00
'       Unable to parse 17:14:48
'       Unable to parse 17:14:48.153
'       '3:17:14:48.153' --> 3.17:14:48.1530000
'       Unable to parse 3:17:14:48.153
'       '3:17:14:48,153' --> 3.17:14:48.1530000
'       '12' --> 12.00:00:00
'       '12' --> 12:00:00
'       '12' --> 00:00:12

Keterangan

Metode ini TryParseExact(String, String, IFormatProvider, TimeSpan) menguraikan representasi string dari interval waktu, yang harus dalam format yang ditentukan oleh format parameter, kecuali bahwa karakter spasi putih di awal dan akhir diabaikan. Metode ini mirip ParseExact(String, String, IFormatProvider) dengan metode , kecuali bahwa metode ini tidak memberikan pengecualian jika konversi gagal.

Parameter format adalah string yang berisi penentu format standar tunggal, atau satu atau beberapa penentu format kustom yang menentukan format yang diperlukan dari input. Untuk informasi selengkapnya tentang string format yang valid, lihat String Format Rentang Waktu Standar dan String Format Rentang Waktu Kustom.

Penting

Metode ini TryParseExact(String, String, IFormatProvider, TimeSpan) menggunakan konvensi budaya yang ditentukan oleh formatProvider parameter hanya jika format merupakan string format standar TimeSpan yang nilainya adalah "g" atau "G". String format standar "c", "t", dan "T" menggunakan konvensi pemformatan budaya invarian. String format kustom menentukan format yang tepat dari string input dan menggunakan karakter literal untuk memisahkan komponen interval waktu.

Parameter formatProvider adalah IFormatProvider implementasi yang menyediakan informasi khusus budaya tentang format string yang dikembalikan jika format merupakan string format standar. Parameter formatProvider dapat berupa salah satu hal berikut:

Jika formatProvider adalah null, DateTimeFormatInfo objek yang terkait dengan budaya saat ini digunakan.

Lihat juga

Berlaku untuk