sam
sam

Reputation: 2606

DateTimePicker Time validation

I am trying to validate the chosen date by user using DateTimePicker with current time so user cannot choose time less than the current time as shown below

if (DTP_StartTime.Value.TimeOfDay < DateTime.Today.TimeOfDay)
{
    MessageBox.Show("you cannot choose time less than the current time",
                    "Message",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.RtlReading);
}

But it now shown so for testing purpose I tried to display message to see what the value of these to condition and found that DateTime.Today.Date value is 00:00:00

MessageBox.Show(DTP_StartTime.Value.TimeOfDay +" <> "+ DateTime.Today.TimeOfDay);

Is that the correct way to validate the time?

Upvotes: 1

Views: 1390

Answers (3)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131305

DateTime.Today returns the current date. If you want the current time you should use DateTime.Now.

DateTime values can be compared directly, they don't have to be converted to strings.

As for validation, just don't allow the user to select a past timeby setting the DateTimePicker.MinimumDateTime property to DateTime.Now before displaying the form, eg :

DTP_SessionDate.MinimumDateTime=DateTime.Now;

There's still a chance that a user can take too long to enter a time and enter a time a few seconds or minutes in the past. You can still cover this by setting the minimum 1-2 minutes in the future:

DTP_SessionDate.MinimumDateTime=DateTime.Now.AddMinutes(1);

In any case, you can validate the value in code with

if(DTP_SessionDate.Value < DateTime.Now)
{
    MessageBox.Show("you cannot choose time less than the current time",
                ...);
}

An even better option though would be to use the validation features of the stack you use. All of the .NET stacks, Winforms, WPF, ASP.NET, provide input validation either through validators, validation properties or validation events

User Input validation in Windows Forms explains the mechanism available to validate input on the Windows Forms stack.

These events, combined with error providers are used to display the exclamation marks and error messages typically shown in data entry forms.

DateTimePicker has a Validating event that can be used to validate user input and prevent the user from entering any values in the past. The example in the event's documentation could be adapted to this :

private void DTP_SessionDate_Validating(object sender, 
            System.ComponentModel.CancelEventArgs e)
{
    if(DTP_SessionDate.Value < DateTime.Now)
    {
        e.Cancel = true;
        DTP_SessionDate.Value=DateTime.Now;

        // Set the ErrorProvider error with the text to display. 
        this.errorProvider1.SetError(DTP_SessionDate, "you cannot choose time less than the current time");
     }
}

private void DTP_SessionDate_Validated(object sender, System.EventArgs e)
{
   // If all conditions have been met, clear the ErrorProvider of errors.
   errorProvider1.SetError(DTP_SessionDate, "");
}

The article How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component and the other articles in that section explain how that control works and how you can combine it with other controls

Update

If you want to only validate the time, you can use the DateTime.TimeOfDay property :

if(DTP_SessionDate.Value.TimeOfDay < DateTime.Now.TimeOfDay)

Upvotes: 1

Tatranskymedved
Tatranskymedved

Reputation: 4371

You shouldn't work with the Date, as it represents only "DATE", not "TIME". The property You want to use is called Now, which includes also the time.

if(DTP_SessionDate.Value < DateTime.Now)
{  ... }

Update

As requsted, if You are working only with the Time of the day,You can refer to it like this:

if(DTP_SessionDate.Value.TimeOfDay < DateTime.Now.TimeOfDay)
{  ... }

DateTime.Today doesn't hold information about time. Only date. DateTime.Now however includes information about time.

Upvotes: 1

Akaino
Akaino

Reputation: 1025

As per Microsoft Docs that is what DateTime returns.

You can however use DateTime.Now to get your date and time.

Upvotes: 0

Related Questions