Parse hour:minute:second value with "G" specifier.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture;
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);
}
}
}
Related examples in the same category