I want to format a typed in time to a specific standard:
private String CheckTime(String value)
{
String[] formats = { "HH mm", "HHmm", "HH:mm", "H mm", "Hmm", "H:mm", "H" };
DateTime expexteddate;
if (DateTime.TryParseExact(value, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out expexteddate))
return expexteddate.ToString("HH:mm");
else
throw new Exception(String.Format("Not valid time inserted, enter time like: {0}HHmm", Environment.NewLine));
}
When the user types it like: "09 00", "0900", "09:00", "9 00", "9:00"
But when the user types it like: "900"
or "9"
the system fails to format it, why?
They are default formats I tought.
string str = CheckTime("09:00"); // works
str = CheckTime("900"); // FormatException at TryParseExact