Solved

Validate time value using vb.net

Posted on 2008-10-07
7
4,158 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
ID: 22661201
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
ID: 22661255
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
ID: 22661270
I tried entering 16:22 (4:22 pm) and false is returned.  
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 19

Accepted Solution

by:
Stephen Manderson earned 500 total points
ID: 22661569
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
ID: 22661733
Awesome...that works....thanks
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 22661822
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
ID: 22662142
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 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