Checking string in the particular time format

1k views Asked by At

I'm trying check is string(textBox1.Text) on the time format "hh:mm:ss", for send that string, as time parameter, in Sql query. I use TimeSpan.TryParseExact method:

        TimeSpan check;
        TimeSpan.TryParseExact(textBox1.Text, "hh:mm:ss", null, out check);
        if(check==null)   MessageBox.Show("incorrect time");
        else... 

But when I write to textbox 11 to the table adding the time 00:00:11 (11 seconds). I want so it's was incorrect time, correct is 00:00:11 line. How to do it's right?

3

There are 3 answers

2
keyboardP On BEST ANSWER

You should use controls that are suitable for the task you're trying to do. For example, in this case, it may be easier and more consistent for users to use a DateTimePicker control instead of a TextBox.

Set the DateTimePicker's Format property to Time and the ShowUpDown property to true. You can also do this in code as shown below.

myTimePicker.Format = DateTimePickerFormat.Time;
myTimePicker.ShowUpDown = true;

You can also use the CustomFormat property to format the way you want the time displayed. If you use CustomFormat, you'll want to do this:

myTimePicker.Format = DateTimePickerFormat.Custom; //instead of Time as above
myTimePicker.CustomFormat = "hh:mm:ss"; //or whichever format you want
1
Steve On

You should change your code to use the result of TryParseExact that is a boolean true if the conversion succed. Also notice that there is a standard format string for hours/minutes/seconds

TimeSpan check;
if(TimeSpan.TryParseExact("09:00:01", "g", CultureInfo.CurrentCulture, out check))
    MessageBox.Show("Correct");
else
    MessageBox.Show("Incorrect");

EDIT
Looking at your comment below then probably you need a custom timespan format string

TimeSpan check;
if(TimeSpan.TryParseExact("09:00:01", @"hh\:mm\:ss", CultureInfo.CurrentCulture, out check))
    MessageBox.Show("Correct");
else
    MessageBox.Show("Incorrect");
0
JohnLBevan On

You could do this using regular expressions:

using System.Text.RegularExpressions;
namespace StackOverflow
{
    class Demo
    {
        /* ... */
        readonly Regex timeFormat = new Regex(@"^(?:(?:[0-1][0-9])|2[0-3]):[0-5][0-9]:[0-5][0-9]$", RegexOptions.Compiled);
        public bool IsTimeFormatValid(string time)
        {
            return timeFormat.IsMatch(time);
        }
        /* ... */
    }
}

Regex Example: http://regex101.com/r/qR3qV6/1

NB: Timespan.TryParse aims to convert a string to a timespan. TryParseExact does the same; only it allows you to specify a culture/expected format (it's poorly named as it doesn't force the format to be exactly what you've specified).

These methods want to convert if possible; so they're quite generous in what they'll accept. They're not designed to validate the format; only to make a best attempt at converting the given data based on a given format. In this case given a string lacking information they're assuming that hours and minutes not being specified means they're 0 valued, so they treat your data as seconds.

Using a regex avoids this issue - it says you want to check the format rather than just see if the system can make a guess at what the data in that format means.