Parse string to TimeSpan in CSharp

Description

The following code shows how to parse string to TimeSpan.

Example


using System.Threading;
using System;/*w w w  . java  2  s .c  o m*/
using System.Globalization;


public class MainClass
{
    public static void Main(String[] argv){  
        
    string[] values = { "000000006", "12.12:12:12.12345678",
                       "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" };
    foreach (string value in values)
    {
        TimeSpan interval = TimeSpan.Parse(value);
        Console.WriteLine("{0} --> {1}", value, interval);
    }
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var