Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time 24 hour format

Status
Not open for further replies.

developer155

Programmer
Joined
Jan 21, 2004
Messages
512
Location
US
Hi, I need a way to make sure that user entered time is in 24 hour format, like 23:55 for 11:55 PM
I got stuck, my code is below. Thanks for any help

public bool ValidTime(string strTime)
{
bool lValid = false;
if(strTime != null && strTime != "")
{
try
{
System.Globalization.DateTimeFormatInfo dateFormat = new System.Globalization.DateTimeFormatInfo();
dateFormat.LongTimePattern = "HH:mm:ss";

DateTime dt = DateTime.Parse(strTime, dateFormat);

lValid = true;
}
catch
{
// we failed
lValid = false;
}
}
else
{
lValid = true;
}
return lValid;
}
 
OTTOMH:

Code:
DateTime test;
System.Globalization.DateTimeFormatInfo dateFormat = new System.Globalization.DateTimeFormatInfo();
dateFormat.LongTimePattern  = "HH:mm:ss";
[COLOR=red][b]return DateTime.TryParse(strTime, dateFormat, out test);[/b][/color]

but you'd be better off using regular expressions

mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top