Solved

Validate time value using vb.net

Posted on 2008-10-07
7
4,369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

719 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