Mike_Stevens
asked on
Validate time value using vb.net
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
I am hoping somone can help. Thanks
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
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
ASKER
I tried entering 16:22 (4:22 pm) and false is returned.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome...that works....thanks
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
Check the following links...
Using javascript: http://www.codeproject.com/KB/scripting/timevalidation.aspx
https://www.experts-exchange.com/questions/22718752/Check-if-string-is-valid-time.html
Regards,
M.Raja
Using javascript: http://www.codeproject.com/KB/scripting/timevalidation.aspx
https://www.experts-exchange.com/questions/22718752/Check-if-string-is-valid-time.html
Regards,
M.Raja
Open in new window