Output of this code will always be false,even if I pass date in correct format.Please help me... Here the 2 Parameters passed are time and format, ie("HHMMSS" format).
static bool ValidateTime(string time, string format)
{
try
{
//time = time.Replace(":","");
System.Globalization.DateTimeFormatInfo tinfo = new System.Globalization.DateTimeFormatInfo();
tinfo.LongTimePattern = format;
DateTime dt = DateTime.ParseExact(time, "format", tinfo);
if (dt.Hour != null)
{
}
return true;
}
catch (Exception e)
{
return false;
}
}
static bool ValidateTime(string time, string format)
{
DateTime outTime;
return DateTime.TryParseExact(time, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out outTime);
}
Remember that you should use "HHmmss" format string to validate 24-hour time.
Custom Date and Time Format Strings (MSDN)
The following code works. You'll have to tweak a bit and add the method signatures.
string time = "201555";
string format = "HHmmss";
bool ok = false;
try
{
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
DateTime dt = DateTime.ParseExact(time, format, provider);
if (dt.Hour != null)
{
ok = true;
}
}
catch (Exception e)
{
//// ok = false; // already setup in initializer above.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With