Solved

Validate time value using vb.net

Posted on 2008-10-07
7
4,081 Views
Last Modified: 2013-11-08
I am looking for a method to validate a time value to make sure it is a valid time.   I have found several examples but i need to validate both the 24 hour format (hh:mm) or (h:mm am/pm) format.   I cannot find any examples of how to do both.   I am using vb.net with 3.5 version framework.

I am hoping somone can help.  Thanks
0
Comment
Question by:Mike_Stevens
7 Comments
 
LVL 19

Expert Comment

by:Stephen Manderson
Comment Utility
Hi there the below function should do the trick
    Public Function IsValidTime(ByVal TheTime As String) As Boolean

        ' Format 1 validates the time against the 12 hour format

        Dim Format1 As String = "^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$"

        Dim TryValidateFormat1 As New System.Text.RegularExpressions.Regex(Format1)

        ' Format 2 validates the 24 hour format

        Dim Format2 As String = "^[0-2][0-3][:][0-5][0-9]$"

        Dim TryValidateFormat2 As New System.Text.RegularExpressions.Regex(Format2)
 

        Return TryValidateFormat1.IsMatch(TheTime) Or TryValidateFormat2.IsMatch(TheTime)
 

    End Function

Open in new window

0
 
LVL 19

Expert Comment

by:Stephen Manderson
Comment Utility
Tested against the following

Is 11:00 valid : ? True
Is 1:00 PM valid : ? True
Is 11:61 valid : ? False
Is 13:00 PM valid : ? False
Is 143:00 valid : ? False
Is 1:00 ZM valid : ? False
Is 23:60 valid : ? False

Regards
Steve
0
 

Author Comment

by:Mike_Stevens
Comment Utility
I tried entering 16:22 (4:22 pm) and false is returned.  
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 19

Accepted Solution

by:
Stephen Manderson earned 500 total points
Comment Utility
Change the second format to

Dim Format2 As String = "([0-1][0-9]|2[0-3]):([0-5][0-9])"

Regards
Steve
0
 

Author Comment

by:Mike_Stevens
Comment Utility
Awesome...that works....thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Why not just use DateTime.TryParse()?....
        Dim dt As DateTime

        Dim strTime As String
 

        strTime = "16:22"

        If DateTime.TryParse(strTime, dt) Then

            Debug.Print(dt.ToString("t"))

        Else

            Debug.Print("Failed")

        End If
 

        strTime = "4:22 pm"

        If DateTime.TryParse(strTime, dt) Then

            Debug.Print(dt.ToString("t"))

        Else

            Debug.Print("Failed")

        End If

Open in new window

0
 
LVL 14

Expert Comment

by:raja_ind82
Comment Utility
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now