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 27
Eddie ShipmanAll-around developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try the following:
DateTime.ParseExact("3/28/2006 12:00:00 AM", "M/d/yyyy h:m:s tt", System.Globalization.CultureInfo.InvariantCulture)
Eddie ShipmanAll-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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.