Aracılığıyla paylaş


TimeSpan.Parse Yöntem

Tanım

Bir zaman aralığının dize gösterimini eşdeğerine TimeSpan dönüştürür.

Aşırı Yüklemeler

Name Description
Parse(ReadOnlySpan<Char>, IFormatProvider)

Belirtilen kültüre özgü biçim bilgilerini kullanarak zaman aralığının span gösterimini eşdeğerine TimeSpan dönüştürür.

Parse(String, IFormatProvider)

Belirtilen kültüre özgü biçim bilgilerini kullanarak bir zaman aralığının dize gösterimini eşdeğerine TimeSpan dönüştürür.

Parse(String)

Bir zaman aralığının dize gösterimini eşdeğerine TimeSpan dönüştürür.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs

Belirtilen kültüre özgü biçim bilgilerini kullanarak zaman aralığının span gösterimini eşdeğerine TimeSpan dönüştürür.

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

Parametreler

input
ReadOnlySpan<Char>

Dönüştürülecek zaman aralığını temsil eden karakterleri içeren bir yayılma alanı.

formatProvider
IFormatProvider

Kültüre özgü biçimlendirme bilgileri sağlayan bir nesne.

Döndürülenler

tarafından formatProviderbelirtildiği gibi öğesine inputkarşılık gelen bir zaman aralığı.

Uygulamalar

Şunlara uygulanır

Parse(String, IFormatProvider)

Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs

Belirtilen kültüre özgü biçim bilgilerini kullanarak bir zaman aralığının dize gösterimini eşdeğerine TimeSpan dönüştürür.

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

Parametreler

input
String

Dönüştürülecek zaman aralığını belirten dize.

formatProvider
IFormatProvider

Kültüre özgü biçimlendirme bilgileri sağlayan bir nesne.

Döndürülenler

tarafından formatProviderbelirtildiği gibi öğesine inputkarşılık gelen bir zaman aralığı.

Uygulamalar

Özel durumlar

input, null'e eşittir.

input geçersiz bir biçime sahip.

input TimeSpan.MinValue değerinden küçük veya TimeSpan.MaxValue değerinden büyük bir sayıyı temsil eder.

-veya-

içindeki input gün, saat, dakika veya saniye bileşenlerinden en az biri geçerli aralığının dışındadır.

Örnekler

Aşağıdaki örnek bir nesne dizisini CultureInfo tanımlar ve bir dize dizisindeki öğeleri ayrıştırmak için yöntemine yapılan çağrılardaki Parse(String, IFormatProvider) her nesneyi kullanır. Örnekte, belirli bir kültürün kurallarının biçimlendirme işlemini nasıl etkilediği gösterilmektedir.

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
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

' 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

Açıklamalar

Bu yöntem, tarafından formatProviderbelirtilen kültür için kültüre özgü biçimlerin her birini kullanarak ayrıştırmaya input çalışır.

formatProvider parametresi, döndürülen dizenin biçimi hakkında kültüre özgü bilgiler sağlayan bir IFormatProvider uygulamadır. formatProvider parametresi aşağıdakilerden biri olabilir:

Eğer formatProvidernull ise, geçerli kültürle ilişkili olan DateTimeFormatInfo nesnesi kullanılır.

Bu API hakkında daha fazla bilgi için bkz. System.TimeSpan.Parse için ek API açıklamaları.

Şunlara uygulanır

Parse(String)

Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs
Kaynak:
TimeSpan.cs

Bir zaman aralığının dize gösterimini eşdeğerine TimeSpan dönüştürür.

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

Parametreler

s
String

Dönüştürülecek zaman aralığını belirten dize.

Döndürülenler

öğesine karşılık gelen sbir zaman aralığı.

Özel durumlar

s, null'e eşittir.

s geçersiz bir biçime sahip.

s TimeSpan.MinValue değerinden küçük veya TimeSpan.MaxValue değerinden büyük bir sayıyı temsil eder.

-veya-

Gün, saat, dakika veya saniye bileşenlerinden en az biri geçerli aralığının dışındadır.

Örnekler

Aşağıdaki örnek, bir dize dizisindeki her öğeyi bir TimeSpan değere dönüştürmek için yöntemini kullanırParse. Mevcut sistem kültürünün ayrıştırma işlemini nasıl etkilediğini göstermek için mevcut sistem kültürünü Hırvat - Hırvatistan ("hr-HR") ve İngilizce - ABD ("en-US") olarak değiştiriyor.

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
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

' 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

Açıklamalar

Bu API hakkında daha fazla bilgi için bkz. TimeSpan.Parse için ek API açıklamaları.

Şunlara uygulanır