Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Validate time value using vb.net

Posted on 2008-10-07
7
Medium Priority
?
4,653 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 19

Accepted Solution

by:
Stephen Manderson earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

609 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