Validating Times to be between certain hours

Posted on 2008-11-06
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? code would be appreciated.
Question by:EddieShipman
    LVL 4

    Expert Comment

    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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now