Validating Times to be between certain hours

I am using a DevExpress.XtraEditors.TimeEdit control and it has a property named EditValue that is storing its value in this format: "3/28/2006 12:00:00 AM"

Now, I am using a Validating event to validate that the time entered is > 7:00AM and < 7:00PM but don't know how I can parse the evit value to make sure it is between those values. If I use Date.TryParse, it puts the HOUR in 12 hour format, not 24 so I can't tell if it's 3:00AM or 3:00PM.

Any ideas? code would be appreciated.
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
Eddie ShipmanConnect With a Mentor All-around developerAuthor Commented:
Oops, found the answer on the DevEx site. This code essentially sets a min and max time
for the two controls I have.
Private minTime As New DateTime(1, 1, 1, 6, 30, 0)
    Private maxTime As New DateTime(1, 1, 1, 19, 0, 0)
    Private Sub dtpTimeStart_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles dtpTimeStart.Validating, dtpTimeEnd.Validating
        Dim edit As DevExpress.XtraEditors.TimeEdit = TryCast(sender, DevExpress.XtraEditors.TimeEdit)
        If edit.EditValue IsNot Nothing AndAlso (edit.Time.TimeOfDay > maxTime.TimeOfDay Or _
                                                edit.Time.TimeOfDay < minTime.TimeOfDay) Then
            RemoveHandler dtpTimeStart.EditValueChanged, AddressOf dtpTimeStart_EditValueChanged
            MessageBox.Show("The Time you entered for the appointment is out of range. It must be between 6:30AM and 7:00PM")
            If edit.Name = "dtpStart" Then
                edit.Time = edit.Time.[Date].AddTicks(minTime.TimeOfDay.Ticks)
                dtpTimeEnd.Time = edit.Time.[Date].AddTicks(minTime.TimeOfDay.Ticks)
                edit.Time = edit.Time.[Date].AddTicks(maxTime.TimeOfDay.Ticks)
            End If
            AddHandler dtpTimeStart.EditValueChanged, AddressOf dtpTimeStart_EditValueChanged
        End If
    End Sub

Open in new window

Try the following:
DateTime.ParseExact("3/28/2006 12:00:00 AM", "M/d/yyyy h:m:s tt", System.Globalization.CultureInfo.InvariantCulture)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.