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