Akash Deshpande
Akash Deshpande

Reputation: 2645

How to set max time and min time in c# datetime picker

I am using a datetime picker in c# windows forms application.

How to set the the min time and max time ? I have a string "07:52:22" and I want to set this as the max or min time. How can I do this ?

DatetimePicker.MinDate.TimeOfDay = "07:52:22";

This is wrong but this is what I want.

Upvotes: 2

Views: 9211

Answers (2)

awudoin
awudoin

Reputation: 649

DateTimePicker datePicker = new DateTimePicker;
dateTimePicker.MinDate = DateTime.Parse("7:52:22");

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 942548

Right, not supported. You'll have to add the validation yourself with the ValueChanged event. You could just limit it like this:

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        var max = new TimeSpan(7, 52, 22);
        if (dateTimePicker1.Value.TimeOfDay >= max) {
            dateTimePicker1.Value = dateTimePicker1.Value.Date + max;
        }
    }

Upvotes: 5

Related Questions