Solved

Validate time value using vb.net

Posted on 2008-10-07
7
4,280 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 86

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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