Falconeer
Falconeer

Reputation: 411

How can I limit the DateTimePicker to only change the date, not the time?

Here's the situation:

I have two DateTimePicker controls for the start date and end date in a log viewer. I want the start date to be whatever the user picks, but with a time of 00:00:00. The end time to be the date the user picks, but a time of 23:59:59.999.

(I'll also be writing code the ensure the end date is equal to or greater than the start date, but I can handle that)

What is the best way to implement this?

Upvotes: 3

Views: 1547

Answers (2)

Hans Passant
Hans Passant

Reputation: 942428

Just ignore the time part of the date you get from DTP with DateTime.Date. Like this:

    private void btnOK_Click(object sender, EventArgs e) {
        var start = dtpStart.Value.Date;
        var end = dtpEnd.Value.Date.AddDays(1).AddMilliseconds(-1);
        if (end > start) {
            // etc...
        }
    }

Upvotes: 3

Jay
Jay

Reputation: 57979

Use a DatePicker instead of a DateTimePicker and/or subscribe to the change events and force the time you want every time the value is changed.

Upvotes: 2

Related Questions