TimeSpan.Parse 메서드

정의

시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환합니다.

오버로드

Parse(String)

시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환합니다.

Parse(ReadOnlySpan<Char>, IFormatProvider)

지정된 문화권별 형식 정보를 사용하여 시간 간격에 대한 범위 표현을 해당 TimeSpan으로 변환합니다.

Parse(String, IFormatProvider)

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

Parse(String)

시간 간격에 대한 문자열 표현을 해당 TimeSpan으로 변환합니다.

public:
 static TimeSpan Parse(System::String ^ s);
public static TimeSpan Parse (string s);
static member Parse : string -> TimeSpan
Public Shared Function Parse (s As String) As TimeSpan

매개 변수

s
String

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

반환

s에 해당하는 시간 간격입니다.

예외

s이(가) null인 경우.

s의 형식이 잘못된 경우.

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

또는

일, 시, 분 또는 초 구성 요소 중 적어도 하나가 유효 범위를 벗어난 경우.

예제

다음 예제에서는 메서드를 Parse 사용하여 문자열 배열의 각 요소를 값으로 TimeSpan 변환합니다. 현재 시스템 문화권을 크로아티아어("hr-HR") 및 영어 -미국("en-US")로 변경하여 현재 시스템 문화가 구문 분석 작업에 미치는 영향을 보여 줍니다.

using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", 
                          "6.12:14:45", "6:12:14:45.3448", 
                          "6:12:14:45,3448", "6:34:14:45" };
      string[] cultureNames = { "hr-HR", "en-US"};
      
      // Change the current culture.
      foreach (string cultureName in cultureNames)
      {
         Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
         Console.WriteLine("Current Culture: {0}", 
                           Thread.CurrentThread.CurrentCulture.Name);
         foreach (string value in values)
         {
            try {
               TimeSpan ts = TimeSpan.Parse(value);
               Console.WriteLine("{0} --> {1}", value, ts.ToString("c"));
            }
            catch (FormatException) {
               Console.WriteLine("{0}: Bad Format", value);
            }   
            catch (OverflowException) {
               Console.WriteLine("{0}: Overflow", value);
            }
         } 
         Console.WriteLine();                                
      }
   }
}
// The example displays the following output:
//    Current Culture: hr-HR
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448: Bad Format
//    6:12:14:45,3448 --> 6.12:14:45.3448000
//    6:34:14:45: Overflow
//    
//    Current Culture: en-US
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448 --> 6.12:14:45.3448000
//    6:12:14:45,3448: Bad Format
//    6:34:14:45: Overflow
open System
open System.Globalization
open System.Threading

let values = 
    [| "6"; "6:12"; "6:12:14"; "6:12:14:45" 
       "6.12:14:45"; "6:12:14:45.3448" 
       "6:12:14:45,3448"; "6:34:14:45" |]
let cultureNames = [| "hr-HR"; "en-US" |]

// Change the current culture.
for cultureName in cultureNames do
    Thread.CurrentThread.CurrentCulture <- CultureInfo cultureName
    printfn $"Current Culture: {Thread.CurrentThread.CurrentCulture.Name}" 
    for value in values do
        try 
            let ts = TimeSpan.Parse value
            printfn $"{value} --> {ts:c}"
        with 
        | :? FormatException ->
            printfn $"{value}: Bad Format"
        | :? OverflowException ->
            printfn $"{value}: Overflow"
    printfn ""                                
// The example displays the following output:
//    Current Culture: hr-HR
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448: Bad Format
//    6:12:14:45,3448 --> 6.12:14:45.3448000
//    6:34:14:45: Overflow
//    
//    Current Culture: en-US
//    6 --> 6.00:00:00
//    6:12 --> 06:12:00
//    6:12:14 --> 06:12:14
//    6:12:14:45 --> 6.12:14:45
//    6.12:14:45 --> 6.12:14:45
//    6:12:14:45.3448 --> 6.12:14:45.3448000
//    6:12:14:45,3448: Bad Format
//    6:34:14:45: Overflow
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim values() As String = { "6", "6:12", "6:12:14", "6:12:14:45", 
                                 "6.12:14:45", "6:12:14:45.3448", 
                                 "6:12:14:45,3448", "6:34:14:45" }
      Dim cultureNames() As String = { "hr-HR", "en-US"}
      
      ' Change the current culture.
      For Each cultureName As String In cultureNames
         Thread.CurrentThread.CurrentCulture = New CultureInfo(cultureName)
         Console.WriteLine("Current Culture: {0}", 
                           Thread.CurrentThread.CurrentCulture.Name)
         For Each value As String In values
            Try
               Dim ts As TimeSpan = TimeSpan.Parse(value)
               Console.WriteLine("{0} --> {1}", value, ts.ToString("c"))
            Catch e As FormatException
               Console.WriteLine("{0}: Bad Format", value)
            Catch e As OverflowException
               Console.WriteLine("{0}: Overflow", value)
            End Try      
         Next 
         Console.WriteLine()                                
      Next
   End Sub
End Module
' The example displays the following output:
'       Current Culture: hr-HR
'       6 --> 6.00:00:00
'       6:12 --> 06:12:00
'       6:12:14 --> 06:12:14
'       6:12:14:45 --> 6.12:14:45
'       6.12:14:45 --> 6.12:14:45
'       6:12:14:45.3448: Bad Format
'       6:12:14:45,3448 --> 6.12:14:45.3448000
'       6:34:14:45: Overflow
'       
'       Current Culture: en-US
'       6 --> 6.00:00:00
'       6:12 --> 06:12:00
'       6:12:14 --> 06:12:14
'       6:12:14:45 --> 6.12:14:45
'       6.12:14:45 --> 6.12:14:45
'       6:12:14:45.3448 --> 6.12:14:45.3448000
'       6:12:14:45,3448: Bad Format
'       6:34:14:45: Overflow

설명

이 API에 대한 자세한 내용은 TimeSpan.Parse에 대한 추가 API 설명을 참조하세요.

적용 대상

Parse(ReadOnlySpan<Char>, IFormatProvider)

지정된 문화권별 형식 정보를 사용하여 시간 간격에 대한 범위 표현을 해당 TimeSpan으로 변환합니다.

public static TimeSpan Parse (ReadOnlySpan<char> input, IFormatProvider? formatProvider = default);
public static TimeSpan Parse (ReadOnlySpan<char> input, IFormatProvider formatProvider = default);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As ReadOnlySpan(Of Char), Optional formatProvider As IFormatProvider = Nothing) As TimeSpan

매개 변수

input
ReadOnlySpan<Char>

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

formatProvider
IFormatProvider

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

반환

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

구현

적용 대상

Parse(String, IFormatProvider)

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

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

매개 변수

input
String

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

formatProvider
IFormatProvider

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

반환

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

구현

예외

input이(가) null인 경우.

input의 형식이 잘못된 경우.

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

또는

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

예제

다음 예제에서는 개체의 CultureInfo 배열을 정의하고 메서드 호출에서 각 개체를 Parse(String, IFormatProvider) 사용하여 문자열 배열의 요소를 구문 분석합니다. 이 예제에서는 특정 문화권의 규칙이 서식 지정 작업에 미치는 영향을 보여 줍니다.

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", 
                          "6.12:14:45", "6:12:14:45.3448", 
                          "6:12:14:45,3448", "6:34:14:45" };
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("ru-RU"),
                                 CultureInfo.InvariantCulture };
      
      string header = String.Format("{0,-17}", "String");
      foreach (CultureInfo culture in cultures)
         header += culture.Equals(CultureInfo.InvariantCulture) ? 
                      String.Format("{0,20}", "Invariant") :
                      String.Format("{0,20}", culture.Name);
      Console.WriteLine(header);
      Console.WriteLine();
      
      foreach (string value in values)
      {
         Console.Write("{0,-17}", value);
         foreach (CultureInfo culture in cultures)
         {
            try {
               TimeSpan ts = TimeSpan.Parse(value, culture);
               Console.Write("{0,20}", ts.ToString("c"));
            }
            catch (FormatException) {
               Console.Write("{0,20}", "Bad Format");
            }   
            catch (OverflowException) {
               Console.Write("{0,20}", "Overflow");
            }      
         }
         Console.WriteLine();                                
      }
   }
}
// The example displays the following output:
//    String                          en-US               ru-RU           Invariant
//    
//    6                          6.00:00:00          6.00:00:00          6.00:00:00
//    6:12                         06:12:00            06:12:00            06:12:00
//    6:12:14                      06:12:14            06:12:14            06:12:14
//    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
//    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
//    6:34:14:45                   Overflow            Overflow            Overflow
open System
open System.Globalization
open System.Text.RegularExpressions

let values = 
    [| "6"; "6:12"; "6:12:14"; "6:12:14:45" 
       "6.12:14:45"; "6:12:14:45.3448"
       "6:12:14:45,3448"; "6:34:14:45" |]
let cultures = 
    [| CultureInfo "en-US" 
       CultureInfo "ru-RU"
       CultureInfo.InvariantCulture |]

let mutable header = $"""{"String",-17}"""
for culture in cultures do
    header <- header +
        if culture.Equals CultureInfo.InvariantCulture then 
            $"""{"Invariant",20}"""
        else
            $"{culture.Name,20}"
printfn $"{header}\m"

for value in values do
    printf $"{value,-17}"
    for culture in cultures do
        try
            let ts = TimeSpan.Parse(value, culture)
            printf $"{ts,20:c}"
        with
        | :? FormatException ->
            printf $"""{"Bad Format",20}"""
        | :? OverflowException ->
            printf $"""{"Overflow",20}"""
    printfn ""                      
// The example displays the following output:
//    String                          en-US               ru-RU           Invariant
//    
//    6                          6.00:00:00          6.00:00:00          6.00:00:00
//    6:12                         06:12:00            06:12:00            06:12:00
//    6:12:14                      06:12:14            06:12:14            06:12:14
//    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
//    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
//    6:34:14:45                   Overflow            Overflow            Overflow
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim values() As String = { "6", "6:12", "6:12:14", "6:12:14:45", 
                                 "6.12:14:45", "6:12:14:45.3448", 
                                 "6:12:14:45,3448", "6:34:14:45" }
      Dim cultures() As CultureInfo = { New CultureInfo("en-US"), 
                                        New CultureInfo("ru-RU"),
                                        CultureInfo.InvariantCulture }
      
      Dim header As String = String.Format("{0,-17}", "String")
      For Each culture As CultureInfo In cultures
         header += If(culture.Equals(CultureInfo.InvariantCulture), 
                      String.Format("{0,20}", "Invariant"),
                      String.Format("{0,20}", culture.Name))
      Next
      Console.WriteLine(header)
      Console.WriteLine()
      
      For Each value As String In values
         Console.Write("{0,-17}", value)
         For Each culture As CultureInfo In cultures
            Try
               Dim ts As TimeSpan = TimeSpan.Parse(value, culture)
               Console.Write("{0,20}", ts.ToString("c"))
            Catch e As FormatException
               Console.Write("{0,20}", "Bad Format")
            Catch e As OverflowException
               Console.Write("{0,20}", "Overflow")
            End Try      
         Next
         Console.WriteLine()                                
      Next
   End Sub
End Module
' The example displays the following output:
'    String                          en-US               ru-RU           Invariant
'    
'    6                          6.00:00:00          6.00:00:00          6.00:00:00
'    6:12                         06:12:00            06:12:00            06:12:00
'    6:12:14                      06:12:14            06:12:14            06:12:14
'    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
'    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
'    6:12:14:45.3448    6.12:14:45.3448000          Bad Format  6.12:14:45.3448000
'    6:12:14:45,3448            Bad Format  6.12:14:45.3448000          Bad Format
'    6:34:14:45                   Overflow            Overflow            Overflow

설명

이 메서드는 input 에 지정된 formatProvider문화권에 대해 각 문화권별 형식을 사용하여 구문 분석을 시도합니다.

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

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

이 API에 대한 자세한 내용은 System.TimeSpan.Parse에 대한 추가 API 설명을 참조하세요.

적용 대상