The easiest way is to use DateTime.TryParseExact:
DateTime time;
bool valid = DateTime.TryParseExact(text,
"HHmmss",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out time);
Note that “M” is months, while “m” is minutes and “s” are seconds; "HH" is a clock in a 24-hour clock instead of an "hh" that will use a 12-hour clock (usually with the am / pm indicator elsewhere).
DateTimeStyles.Nonesays it uses default options. This will use today's date as the date for the time. You can specify DateTimeStyles.NoCurrentDateDefaultwhich January 1 will use 1AD instead.
valid - false, time DateTime.MinValue.
, .NET 4, - TimeSpan.TryParseExact:
TimeSpan time;
bool valid = TimeSpan.TryParseExact(text,
"hhmmss",
CultureInfo.InvariantCulture,
out time);
"hh" "HH" . . Custom TimeSpan format MSDN, , .NET 4.