ASP.NET (C#) Question

Checking wether a user typed in correct formatted time

How do one go about checking wether or not the user typed in a valid time like "hh:mm:ss" what ive tried is:

string format = "hh:mm:ss";
static bool ValidateTime(string time, string format)
DateTime outTime;
return DateTime.TryParseExact(time, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out outTime);

then checking wether it returnes true or false:

bool result = ValidateTime(redigerINDtextbox.Text, format);

if (result)
redigerINDtextbox.Text = "yay";
redigerINDtextbox.Text = "nay";

It always returns false, when parsing in "7:17:05" or even "14:23:23" for example.

Answer Source

hh is for 12-hour clock, so "14:23:23" can't be parsed. For 24-hour clock, use HH:

string format = "HH:mm:ss";