Validating Times to be between certain hours

Posted on 2008-11-06
Medium Priority
Last Modified: 2012-05-05
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? VB.net code would be appreciated.
Question by:Eddie Shipman

Expert Comment

ID: 22900183
Try the following:
DateTime.ParseExact("3/28/2006 12:00:00 AM", "M/d/yyyy h:m:s tt", System.Globalization.CultureInfo.InvariantCulture)
LVL 26

Accepted Solution

Eddie Shipman earned 0 total points
ID: 22900206
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


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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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