Question Verify a String uses Proper Time Format

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
I have a string which opens an Input Box and asks to specify a time..for example: 13:00:00 PM

I'm trying to setup an IF statement that will check to make sure the user has input the time exact like it's formatted above. I'm a little confused though..

VB.NET:
presetAstart = InputBox("Enter Your Desired Start Time" & vbCrLf & "1AM = 01:00:00 AM and 1PM = 13:00:00 PM" & vbCrLf & vbCrLf & "Do not change the formatting. It MUST be: MM-DD-YYYY HH:MM:SS AM/PM", _
                 "Scheduler Preset A Stop Time", "13:00:00 PM")

If presetAstart = Now.ToString("hh-mm-ss") then
'do something
Else
MsgBox("Incorrect")
End If

I imagine it would be something like the above..but how would I check to make sure it's either AM/PM?

Thanks!
 
There's no such time as 13:00:00 PM. There is 1:00:00 PM and there is 13:00:00. Either you use 12-hour time with an AM or PM or you use 24-hour time. You don't use a bastardised combination of the two.

I have to most strongly recommend that you don't use InputBox at all... ever, but especially here. If you did what you should and created your own dialogue form then you could put a DateTimePicker on it and there would be no need to validate at all. The control would do it for you.

If you really want to validate a String for a specific date and/or time format then you should use the Date.TryParseExact method. That would be a poor option in this case though, given that you shouldn't have a String in the first place.
 
(ah, I see that I got beat to this, but I will post anyway since I have written it now)

Hi,

You should never use the InputBox Function in .NET, or any other technology for that matter, since it is an old and antiquated method for getting information into your project.

The best way to do what you want is to create your own Dialog box which allows you to use your own controls to gather the information that you need.

In your case you should be using a DateTimePicker control, formatted to accept Time only, which then does all the data validation that you need. Here is a quick example:-

1) Create a Form called "frmGetSomeData", being your Custom InputBox (for want of a better word), with a DateTimePicker on it and two Buttons for OK and Cancel and then add this code to that form:-

Public Class frmGetSomeData
 
  Private Sub frmGetSomeData_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    DateTimePicker1.Format = DateTimePickerFormat.Time
    DateTimePicker1.ShowUpDown = True
 
    Me.AcceptButton = btnOK
    Me.CancelButton = btnCancel
  End Sub
 
  Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
  End Sub
End Class


2) Call your New InputBox with the following code, Press OK, and see what happens:-

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Using myTimeForm As New frmGetSomeData
    If myTimeForm.ShowDialog = Windows.Forms.DialogResult.OK Then
      Dim someTime As DateTime = myTimeForm.DateTimePicker1.Value
      MsgBox(someTime.ToLongTimeString)
    End If
  End Using
End Sub


You can now modify this principle anyway you need to actually do what you want.

Hope that helps.

Cheers,

Ian
 
Back
Top