TimeSpan.TryParseExact 메서드

정의

시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환하고, 변환에 성공했는지 여부를 나타내는 값을 반환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

오버로드

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격에 대한 지정된 범위 표현을 해당 TimeSpan으로 변환하고, 변환에 성공했는지 나타내는 값을 반환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 지정된 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

TryParseExact(String, String, IFormatProvider, TimeSpan)

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격에 대한 지정된 범위 표현을 해당 TimeSpan으로 변환하고, 변환에 성공했는지 나타내는 값을 반환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 나타내는 문자를 포함하는 범위입니다.

format
ReadOnlySpan<Char>

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열을 나타내는 문자를 포함하는 범위입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

styles
TimeSpanStyles

input의 스타일을 나타내는 하나 이상의 열거형 값입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

매개 변수

input
String

변환할 시간 간격을 지정하는 문자열입니다.

format
String

input에 필요한 형식을 정의하는 표준 또는 사용자 지정 형식 문자열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

styles
TimeSpanStyles

input의 스타일을 나타내는 하나 이상의 열거형 값입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 메서드를 ParseExact(String, String, IFormatProvider) 사용하여 다양한 형식 문자열 및 문화권을 사용하여 시간 간격의 여러 문자열 표현을 구문 분석합니다. 또한 값을 사용하여 TimeSpanStyles.AssumeNegative 각 문자열을 음수 시간 간격으로 해석합니다. 예제의 출력은 스타일이 TimeSpanStyles.AssumeNegative 사용자 지정 형식 문자열과 함께 사용되는 경우에만 반환 값에 영향을 줍니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture = null;
      
      // Parse hour:minute value with custom format specifier.
      intervalString = "17:14";
      format = "h\\:mm";
      culture = CultureInfo.CurrentCulture;
      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

설명

메서드는 TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에 정의된 format 형식이어야 하는 시간 간격의 문자열 표현을 구문 분석합니다. 이 메서드는 변환이 ParseExact(String, String, IFormatProvider, TimeSpanStyles) 실패할 경우 예외를 throw하지 않는다는 점을 제외하고 메서드와 유사합니다.

format 매개 변수는 단일 표준 형식 지정자 또는 의 필수 형식을 정의하는 하나 이상의 사용자 지정 형식 지정자를 포함하는 문자열입니다input. 유효한 형식 문자열에 대한 자세한 내용은 표준 TimeSpan 형식 문자열 및사용자 지정 TimeSpan 형식 문자열을 참조하세요.

formatProvider 매개 변수는 IFormatProvider 가 표준 형식 문자열인 경우 format 반환된 문자열의 형식에 대한 문화권별 정보를 제공하는 구현입니다. 매개 변수는 formatProvider 다음 중 어느 것일 수 있습니다.

가 이 nullDateTimeFormatInfoformatProvider 현재 문화권과 연결된 개체가 사용됩니다.

매개 변수는 styles 사용자 지정 형식 문자열을 사용하여 구문 분석된 문자열의 해석에 영향을 줍니다. 음수 기호가 있는 경우에만 음수 시간 간격으로 해석되는지input() 또는 항상 음수 시간 간격(TimeSpanStyles.NoneTimeSpanStyles.AssumeNegative)으로 해석되는지 여부를 결정합니다. 가 사용되지 않는 경우 TimeSpanStyles.AssumeNegativeformat 수 시간 간격을 구문 분석하려면 리터럴 음수 기호 기호(예: "\-")를 포함해야 합니다.

추가 정보

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 나타내는 문자를 포함하는 범위입니다.

formats
String[]

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열 배열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

styles
TimeSpanStyles

input의 스타일을 나타내는 하나 이상의 열거형 값입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

매개 변수

input
String

변환할 시간 간격을 지정하는 문자열입니다.

formats
String[]

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열 배열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 메서드를 TryParseExact(String, String[], IFormatProvider, TimeSpan) 호출하여 문자열 배열의 각 요소를 값으로 TimeSpan 변환합니다. 이 예제에서는 프랑스어 - 프랑스("fr-FR") 문화권의 서식 규칙을 사용하여 문자열을 해석합니다. 문자열은 일반적인 짧은 형식 또는 일반 long 형식으로 시간 간격을 나타낼 수 있습니다.

또한 이 예제에서는 시간 간격 구문 분석 메서드가 한 자릿수를 해석하는 방식을 변경합니다. 일반적으로 한 자리는 시간 간격의 일 수로 해석됩니다. 대신 사용자 %h 지정 형식 문자열을 사용하여 한 숫자를 시간 수로 해석합니다. 이 변경 내용이 적용되려면 사용자 지정 서식 문자열이 %h 배열의 다른 형식 문자열 formats 보다 우선해야 합니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "g", "G", "%h"};
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         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

설명

메서드는 TryParseExact(String, String[], IFormatProvider, TimeSpan) 시간 간격의 문자열 표현을 구문 분석합니다. 선행 및 후행 공백 문자는 무시된다는 점을 제외하고 매개 변수로 formats 지정된 형식 문자열 중 하나에 정의된 형식이어야 합니다. 이 메서드는 변환이 ParseExact(String, String[], IFormatProvider) 실패할 경우 예외를 throw하지 않는다는 점을 제외하고 메서드와 유사합니다.

formats 매개 변수는 요소가 단일 표준 형식 지정자 또는 의 필수 형식을 정의하는 하나 이상의 사용자 지정 형식 지정자로 구성된 문자열 배열입니다input. 유효한 형식 문자열에 대한 자세한 내용은 표준 TimeSpan 형식 문자열 및사용자 지정 TimeSpan 형식 문자열을 참조하세요. input 구문 분석 작업이 성공하려면 의 formats 멤버와 정확히 일치해야 합니다. 구문 분석 작업은 배열의 첫 번째 요소 formats 로 시작하는 의 각 요소와 일치 input 하려고 시도합니다.

중요

메서드는 TryParseExact 구문 분석 input 하는 formatProvider 데 사용되는 형식 문자열이 값이 "g" 또는 "G"인 표준 TimeSpan 형식 문자열인 경우에만 매개 변수로 지정된 문화권의 규칙을 사용합니다. "c", "t" 및 "T" 표준 형식 문자열은 고정 문화권의 서식 규칙을 사용합니다. 사용자 지정 형식 문자열은 입력 문자열의 정확한 형식을 정의하고 리터럴 문자를 사용하여 시간 간격의 구성 요소를 구분합니다.

formatProvider 매개 변수는 IFormatProvider 구문 분석 input 하는 데 사용되는 형식 문자열이 표준 형식 문자열인 경우 반환된 문자열의 형식에 대한 문화권별 정보를 제공하는 구현입니다. 매개 변수는 formatProvider 다음 중 어느 것일 수 있습니다.

가 이 nullDateTimeFormatInfoformatProvider 현재 문화권과 연결된 개체가 사용됩니다.

추가 정보

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식, 문화권별 형식 정보 및 스타일을 사용하여 시간 간격의 지정된 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

매개 변수

input
String

변환할 시간 간격을 지정하는 문자열입니다.

formats
String[]

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열 배열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

styles
TimeSpanStyles

input의 스타일을 나타내는 하나 이상의 열거형 값입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 메서드를 TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) 호출하여 문자열 배열의 각 요소를 값으로 TimeSpan 변환합니다. 문자열은 일반적인 짧은 형식 또는 일반 long 형식으로 시간 간격을 나타낼 수 있습니다.

또한 이 예제에서는 시간 간격 구문 분석 메서드가 한 자릿수를 해석하는 방식을 변경합니다. 일반적으로 한 자리는 시간 간격의 일 수로 해석됩니다. 대신 사용자 %h 지정 형식 문자열을 사용하여 한 숫자를 시간 수로 해석합니다. 이 변경 내용이 적용되려면 사용자 지정 서식 문자열이 %h 배열의 다른 형식 문자열 formats 보다 우선해야 합니다. 또한 출력 TimeSpanStyles.AssumeNegative 에서 메서드 호출에 지정된 플래그는 이 형식 지정자를 사용하여 문자열을 구문 분석할 때만 사용됩니다.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "%h", "g", "G" };
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("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

설명

메서드는 TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) 시간 간격의 문자열 표현을 구문 분석합니다. 선행 및 후행 공백 문자는 무시된다는 점을 제외하고 매개 변수로 formats 지정된 형식 문자열 중 하나에 정의된 형식이어야 합니다. 이 메서드는 변환이 ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 실패할 경우 예외를 throw하지 않는다는 점을 제외하고 메서드와 유사합니다.

formats 매개 변수는 요소가 단일 표준 형식 지정자 또는 의 필수 형식을 정의하는 하나 이상의 사용자 지정 형식 지정자로 구성된 문자열 배열입니다input. 유효한 형식 문자열에 대한 자세한 내용은 표준 TimeSpan 형식 문자열 및사용자 지정 TimeSpan 형식 문자열을 참조하세요. input 구문 분석 작업이 성공하려면 의 formats 멤버와 정확히 일치해야 합니다. 구문 분석 작업은 배열의 첫 번째 요소 formats 로 시작하는 의 각 요소와 일치 input 하려고 시도합니다.

중요

메서드는 ParseExact 구문 분석 input 하는 formatProvider 데 사용되는 형식 문자열이 값이 "g" 또는 "G"인 표준 TimeSpan 형식 문자열인 경우에만 매개 변수로 지정된 문화권의 규칙을 사용합니다. "c", "t" 및 "T" 표준 형식 문자열은 고정 문화권의 서식 규칙을 사용합니다. 사용자 지정 형식 문자열은 입력 문자열의 정확한 형식을 정의하고 리터럴 문자를 사용하여 시간 간격의 구성 요소를 구분합니다.

formatProvider 매개 변수는 IFormatProvider 구문 분석 input 하는 데 사용되는 형식 문자열이 표준 형식 문자열인 경우 반환된 문자열의 형식에 대한 문화권별 정보를 제공하는 구현입니다. 매개 변수는 formatProvider 다음 중 어느 것일 수 있습니다.

가 이 nullDateTimeFormatInfoformatProvider 현재 문화권과 연결된 개체가 사용됩니다.

매개 변수는 styles 사용자 지정 형식 문자열을 사용하여 구문 분석되는 문자열의 해석에 영향을 줍니다. 음수 기호가 있는 경우에만 음수 시간 간격으로 해석되는지input() 또는 항상 음수 시간 간격(TimeSpanStyles.NoneTimeSpanStyles.AssumeNegative)으로 해석되는지 여부를 결정합니다. 가 사용되지 않는 경우 TimeSpanStyles.AssumeNegativeformat 수 시간 간격을 구문 분석하려면 리터럴 음수 기호 기호(예: "\-")를 포함해야 합니다.

추가 정보

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표시의 형식은 지정된 형식 중 하나와 정확히 일치해야 합니다.

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

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 나타내는 문자를 포함하는 범위입니다.

formats
String[]

input에 허용되는 형식을 정의하는 표준 또는 사용자 지정 형식 문자열 배열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

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

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격의 지정된 범위 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 나타내는 문자를 포함하는 범위입니다.

format
ReadOnlySpan<Char>

허용 가능한 input 형식을 정의하는 표준 또는 사용자 지정 형식 문자열을 나타내는 문자를 포함하는 범위입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

적용 대상

TryParseExact(String, String, IFormatProvider, TimeSpan)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

지정된 형식 및 문화권별 형식 정보를 사용하여 시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환합니다. 문자열 표현의 형식이 지정된 형식과 정확하게 일치해야 합니다.

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

매개 변수

input
String

변환할 시간 간격을 지정하는 문자열입니다.

format
String

input에 필요한 형식을 정의하는 표준 또는 사용자 지정 형식 문자열입니다.

formatProvider
IFormatProvider

문화권별 형식 정보를 제공하는 개체입니다.

result
TimeSpan

이 메서드가 반환될 때 input에 지정된 시간 간격을 나타내는 개체를 포함하고, 변환이 실패할 경우에는 Zero를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

반환

input이(가) 성공적으로 변환되었으면 true이고, 그렇지 않으면 false입니다.

예제

다음 예제에서는 메서드를 TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) 사용하여 다양한 형식 문자열 및 문화권을 사용하여 시간 간격의 여러 문자열 표현을 구문 분석합니다.

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

설명

메서드는 TryParseExact(String, String, IFormatProvider, TimeSpan) 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에 정의된 format 형식이어야 하는 시간 간격의 문자열 표현을 구문 분석합니다. 이 메서드는 변환이 ParseExact(String, String, IFormatProvider) 실패할 경우 예외를 throw하지 않는다는 점을 제외하고 메서드와 유사합니다.

format 매개 변수는 단일 표준 형식 지정자 또는 의 필수 형식을 정의하는 하나 이상의 사용자 지정 형식 지정자를 포함하는 문자열입니다input. 유효한 형식 문자열에 대한 자세한 내용은 표준 TimeSpan 형식 문자열 및사용자 지정 TimeSpan 형식 문자열을 참조하세요.

중요

메서드는 TryParseExact(String, String, IFormatProvider, TimeSpan) 값이 "g" 또는 "G"인 표준 TimeSpan 형식 문자열인 경우에만 format 매개 변수로 지정된 formatProvider 문화권의 규칙을 사용합니다. "c", "t" 및 "T" 표준 형식 문자열은 고정 문화권의 서식 규칙을 사용합니다. 사용자 지정 형식 문자열은 입력 문자열의 정확한 형식을 정의하고 리터럴 문자를 사용하여 시간 간격의 구성 요소를 구분합니다.

formatProvider 매개 변수는 IFormatProvider 가 표준 형식 문자열인 경우 format 반환된 문자열의 형식에 대한 문화권별 정보를 제공하는 구현입니다. 매개 변수는 formatProvider 다음 중 어느 것일 수 있습니다.

가 이 nullDateTimeFormatInfoformatProvider 현재 문화권과 연결된 개체가 사용됩니다.

추가 정보

적용 대상