TimeSpan.ParseExact 메서드

정의

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

오버로드

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

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

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

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

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

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

ParseExact(String, String[], IFormatProvider)

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

ParseExact(String, String, IFormatProvider)

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

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

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

ParseExact(String, String, IFormatProvider, TimeSpanStyles)

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

public:
 static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

매개 변수

input
String

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

format
String

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

formatProvider
IFormatProvider

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

styles
TimeSpanStyles

input에 나타날 수 있는 스타일 요소를 정의하는 열거형 값의 비트 조합입니다.

반환

format, formatProviderstyles에서 지정한 input에 해당하는 시간 간격입니다.

예외

styles가 잘못된 TimeSpanStyles 값인 경우

input이(가) null인 경우.

input의 형식이 잘못된 경우.

inputTimeSpan.MinValue 보다 작거나 TimeSpan.MaxValue보다 큰 숫자를 나타냅니다.

또는

input의 일, 시, 분 또는 초 구성 요소 중 하나 이상이 유효 범위를 벗어난 경우.

예제

다음 예제에서는 메서드를 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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48";
      format = "g";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153";
      format = @"h\:mm\:ss\.fff";
      culture = null;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
            
      // Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153";
      format = @"d\:hh\:mm\:ss\.fff";
      culture = null;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
   }
}
// The example displays the following output:
//    '17:14' (h\:mm) --> -17:14:00
//    '17:14:48' (g) --> 17:14:48
//    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
//    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
//    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
//    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
//    '12' (c) --> 12.00:00:00
//    '12' (%h) --> -12:00:00
//    '12' (%s) --> -00:00:12
open System
open System.Globalization

do
    // Parse hour:minute value with custom format specifier.
    let intervalString = "17:14"
    let format = "h\\:mm"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hour:minute:second value with "g" specifier.
    let intervalString = "17:14:48"
    let format = "g"
    let culture = CultureInfo.InvariantCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hours:minute.second value with custom format specifier.     
    let intervalString = "17:14:48.153"
    let format = @"h\:mm\:ss\.fff"
    let culture = null
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse days:hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
        
    // Parse days:hours:minute.second value with a custom format specifier.     
    let intervalString = "3:17:14:48.153"
    let format = @"d\:hh\:mm\:ss\.fff"
    let culture = null
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48,153"
    let format = "G"
    let culture = new CultureInfo("fr-FR")
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null, TimeSpanStyles.AssumeNegative)
        printfn $"'{intervalString}' ({format}) --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
// The example displays the following output:
//    '17:14' (h\:mm) --> -17:14:00
//    '17:14:48' (g) --> 17:14:48
//    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
//    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
//    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
//    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
//    '12' (c) --> 12.00:00:00
//    '12' (%h) --> -12:00:00
//    '12' (%s) --> -00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo = Nothing
      
      ' Parse hour:minute value with custom format specifier.
      intervalString = "17:14"
      format = "h\:mm"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48"
      format = "g"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153"
      format = "h\:mm\:ss\.fff"
      culture = Nothing
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153"
      format = "d\:hh\:mm\:ss\.fff"
      culture = Nothing
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   End Sub
End Module
' The example displays the following output:
'    '17:14' (h\:mm) --> -17:14:00
'    '17:14:48' (g) --> 17:14:48
'    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
'    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
'    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
'    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
'    '12' (c) --> 12.00:00:00
'    '12' (%h) --> -12:00:00
'    '12' (%s) --> -00:00:12

설명

메서드는 ParseExact 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에 정의된 format 형식이어야 하는 시간 간격의 문자열 표현을 구문 분석합니다. input 의 형식 format 을 정확히 준수해야 하므로 사용자가 문자열 입력을 시간 간격으로 변환할 때 항상 예외 처리를 사용해야 합니다. 예외 처리를 사용하지 않으려는 경우 메서드를 대신 호출할 TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) 수 있습니다.

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

중요

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

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

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

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

추가 정보

적용 대상

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

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

public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 지정하는 범위입니다.

formats
String[]

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

formatProvider
IFormatProvider

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

styles
TimeSpanStyles

입력에 나타날 수 있는 스타일 요소를 정의하는 열거형 값의 비트 조합입니다.

반환

formats, formatProviderstyles에서 지정한 input에 해당하는 시간 간격입니다.

적용 대상

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

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

public:
 static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

매개 변수

input
String

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

formats
String[]

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

formatProvider
IFormatProvider

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

styles
TimeSpanStyles

입력에 나타날 수 있는 스타일 요소를 정의하는 열거형 값의 비트 조합입니다.

반환

formats, formatProviderstyles에서 지정한 input에 해당하는 시간 간격입니다.

예외

styles가 잘못된 TimeSpanStyles 값인 경우

input이(가) null인 경우.

input의 형식이 잘못된 경우.

inputTimeSpan.MinValue 보다 작거나 TimeSpan.MaxValue보다 큰 숫자를 나타냅니다.

또는

input의 일, 시, 분 또는 초 구성 요소 중 하나 이상이 유효 범위를 벗어난 경우.

예제

다음 예제에서는 메서드를 ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 호출하여 문자열 배열의 각 요소를 값으로 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("de-DE");
      
      // Parse each string in inputs using formats and the de-DE culture.
      foreach (string input in inputs) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture,
                                           TimeSpanStyles.AssumeNegative);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
open System
open System.Globalization

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

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

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

설명

메서드는 ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에 정의된 formats 형식 중 하나여야 하는 시간 간격의 문자열 표현을 구문 분석합니다. input 에 지정된 formats형식 중 하나를 정확히 준수해야 하므로 사용자가 문자열 입력을 시간 간격으로 변환할 때 항상 예외 처리를 사용해야 합니다. 예외 처리를 사용하지 않으려는 경우 메서드를 대신 호출할 TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) 수 있습니다.

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

중요

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

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

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

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

추가 정보

적용 대상

ParseExact(String, String[], IFormatProvider)

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

public:
 static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider? formatProvider);
static member ParseExact : string * string[] * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider) As TimeSpan

매개 변수

input
String

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

formats
String[]

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

formatProvider
IFormatProvider

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

반환

formatsformatProvider에서 지정한 input에 해당하는 시간 간격입니다.

예외

input이(가) null인 경우.

input의 형식이 잘못된 경우.

inputTimeSpan.MinValue 보다 작거나 TimeSpan.MaxValue보다 큰 숫자를 나타냅니다.

또는

input의 일, 시, 분 또는 초 구성 요소 중 하나 이상이 유효 범위를 벗어난 경우.

예제

다음 예제에서는 메서드를 ParseExact(String, String[], IFormatProvider) 호출하여 문자열 배열의 각 요소를 값으로 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) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       1:6:52:35,0625 --> 1.06:52:35.0625000
open System
open System.Globalization

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

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

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

설명

메서드는 ParseExact(String, String, IFormatProvider) 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에 정의된 formats 형식 중 하나여야 하는 시간 간격의 문자열 표현을 구문 분석합니다. 에 input 지정된 formats형식 중 하나를 정확히 준수해야 하므로 사용자가 문자열 입력을 시간 간격으로 변환할 때 항상 예외 처리를 사용해야 합니다. 예외 처리를 사용하지 않으려는 경우 메서드를 대신 호출할 TryParseExact(String, String[], IFormatProvider, TimeSpan) 수 있습니다.

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

중요

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

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

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

추가 정보

적용 대상

ParseExact(String, String, IFormatProvider)

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

public:
 static TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider? formatProvider);
static member ParseExact : string * string * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As TimeSpan

매개 변수

input
String

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

format
String

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

formatProvider
IFormatProvider

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

반환

formatformatProvider에서 지정한 input에 해당하는 시간 간격입니다.

예외

input이(가) null인 경우.

input의 형식이 잘못된 경우.

inputTimeSpan.MinValue 보다 작거나 TimeSpan.MaxValue보다 큰 숫자를 나타냅니다.

또는

input의 일, 시, 분 또는 초 구성 요소 중 하나 이상이 유효 범위를 벗어난 경우.

예제

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

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture;
      
      // Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14";
      format = "g";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", 
                           intervalString, format);
      }                     
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
            
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12
open System
open System.Globalization

do
    // Parse hour:minute value with "g" specifier current culture.
    let intervalString = "17:14"
    let format = "g"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hour:minute:second value with "G" specifier.
    let intervalString = "17:14:48"
    let format = "G"
    let culture = CultureInfo.InvariantCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse days:hours:minute.second value with "G" specifier 
    // and current (en-US) culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo.CurrentCulture
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
        
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48.153"
    let format = "G"
    let culture = CultureInfo "fr-FR"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse days:hours:minute.second value with "G" specifier 
    // and fr-FR culture.     
    let intervalString = "3:17:14:48,153"
    let format = "G"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, culture)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"

    // Parse a single number using the "c" standard format string. 
    let intervalString = "12"
    let format = "c"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%h" custom format string. 
    let format = "%h"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
    
    // Parse a single number using the "%s" custom format string. 
    let format = "%s"
    try
        let interval = TimeSpan.ParseExact(intervalString, format, null)
        printfn $"'{intervalString}' --> {interval}"
    with 
    | :? FormatException ->
        printfn $"'{intervalString}': Bad Format for '{format}'"
    | :? OverflowException ->
        printfn $"'{intervalString}': Overflow"
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo
      
      ' Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14"
      format = "g"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48"
      format = "G"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   End Sub
End Module
' The example displays the following output:
'       '17:14' --> 17:14:00
'       '17:14:48': Bad Format for 'G'
'       '17:14:48.153': Bad Format for 'G'
'       '3:17:14:48.153' --> 3.17:14:48.1530000
'       '3:17:14:48.153': Bad Format for 'G'
'       '3:17:14:48,153' --> 3.17:14:48.1530000
'       '12' --> 12.00:00:00
'       '12' --> 12:00:00
'       '12' --> 00:00:12

설명

메서드는 ParseExact(String, String, IFormatProvider) 선행 및 후행 공백 문자가 무시된다는 점을 제외하고 매개 변수에서 정의한 format 형식이어야 하는 시간 간격의 문자열 표현을 구문 분석합니다. input 의 형식 format 을 정확히 준수해야 하므로 사용자가 문자열 입력을 시간 간격으로 변환할 때 항상 예외 처리를 사용해야 합니다. 예외 처리를 사용하지 않으려는 경우 메서드를 대신 호출할 TryParseExact(String, String, IFormatProvider, TimeSpan) 수 있습니다.

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

중요

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

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

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

추가 정보

적용 대상

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

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

public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

매개 변수

input
ReadOnlySpan<Char>

변환할 시간 간격을 지정하는 범위입니다.

format
ReadOnlySpan<Char>

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

formatProvider
IFormatProvider

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

styles
TimeSpanStyles

input에 나타날 수 있는 스타일 요소를 정의하는 열거형 값의 비트 조합입니다.

반환

formatformatProvider에서 지정한 input에 해당하는 시간 간격입니다.

적용 대상