Inputting Time on a Webform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am looking for an example of how to display a text box and prompt the user
for a time ex. 9:00 am. I would have a start time and an end time and then I
would take the difference to give me the duration of the event. I would like
to provide the user with the least opportunity for screwing up on entering
the time.
 
BillG, Just use a normal asp.net text box and a submit button. In the
page_load event use the following:

try {
DateTime dt = DateTime.Parse(this.TextBox1.Text);
// if you get here, it's a date/time value
}
catch {
// if you get here, the user did not enter anything that could be construed
as a time
}

Now, once you get past these basics. You can use a custom validator that
calls the following:

private bool IsTime(string text)
{
String strTimeFormat =
@"^((0?[1-9]|1[012])(:[0-5]\d){0,2}(\s[apAP][mM]|[apAP][mM])?)$|^([01]\d|2[0-3])(:[0-5]\d){1,2}$";
return Regex.IsMatch(text,strTimeFormat);
}

Best of luck...Chuck
 
BillG,

This is not easy. You need the localized culture of your client. I have
never seen a straight method to get that. You can get the language using the
servervariables. However AFAIK not the culture. Therefore AFAIK you can only
use this, when you are sure that your pages are localy used. And than it is
just converting back from the textbox to datetime and compare those with the
time with what you want to compare it.

I hope this helps,

Cor
 
Back
Top