Try to Parse hour:minute value with custom format specifier in CSharp
Description
The following code shows how to try to Parse hour:minute value with custom format specifier.
Example
using System;//from w w w. j a v a2 s.c o m
using System.Globalization;
public class Example
{
public static void Main()
{
string intervalString, format;
TimeSpan interval;
CultureInfo culture = null;
intervalString = "17:14";
format = "h\\:mm";
culture = CultureInfo.CurrentCulture;
if (TimeSpan.TryParseExact(intervalString, format,
culture, TimeSpanStyles.AssumeNegative, out interval))
Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
else
Console.WriteLine("Unable to parse '{0}' using format {1}",
intervalString, format);
}
}
The code above generates the following result.