Gianni Giordano
Gianni Giordano

Reputation: 143

datetime picker mindate and maxdate

I use this code for to set mindate and maxdate hour in datetimepicker :

oraDa.MinDate = DateTime.Parse("14:00");
oraDa.MaxDate = DateTime.Parse("22:00");`.

My problem is when I want to set another mindate and maxdate for another work shift:

oraDa.MinDate = DateTime.Parse("22:00");
oraDa.MaxDate = DateTime.Parse("6:00");. 

The message error is

mindate must be minor less than maxdate.

Any help please? thanks

Upvotes: 0

Views: 951

Answers (2)

Steve
Steve

Reputation: 216302

A DateTime variable (or property) is always composed by a date part and a time part. If you don't set the date part, it is assumed to be the today date, so your DateTime.Parse code produces the following results

13/06/2017 06:00 is lesser than 13/06/2017 22:00

So you are forced to put the full date and you don't need to parse a string for that.

oraDa.MinDate = DateTime.Today.AddHours(22);
oraDa.MaxDate = DateTime.Today.AddDays(1).AddHours(6);

Upvotes: 1

Mighty Badaboom
Mighty Badaboom

Reputation: 6155

The problem ist that you get today as date when you parse a time. And today 22:00 is less than today 6:00.

You could avoid this problem when using this piece of code

oraDa.MinDate = DateTime.Parse("22:00");
oraDa.MaxDate = DateTime.Parse("6:00").AddDays(1);

For today you would get MinDate = 13.06.2017 22:00 and MaxDate = 14.06.2017 6:00

Upvotes: 2

Related Questions