Click here to Skip to main content
13,086,264 members (57,071 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone!

I just have a quick question for everyone. Is it possible to disable some of the months in a DateTimePicker? I'm doing this program that accepts a date range. However it's maximum range is 6 months. For example, if i choose July in DateTimePicker1, the values in DateTimePicker2 should only be August, September, October, November, December. How will i disable the months January, February, March, April, May and June? Thanks!

Posted 10-Oct-12 23:00pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Something like this would help you:

DateTimePicker1.MinDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, 1)
DateTimePicker1.MaxDate = DateTimePicker1.MinDate.AddMonths(6)

This code will restrict the control to show only the current month and the upcoming 6 months. You can pass first DateTimePicker's month in MinDate .
Janna Dela Cruz 11-Oct-12 20:57pm
Thanks! It works!
Janna Dela Cruz 11-Oct-12 23:42pm
Sir Shahan Ayyub,
May you please help me again? I got the date range the next thing i have to do is print the days of the months that was chosen, i have a running program but it only print first day of the chosen month. I tried to use the System.DateTime.DaysInMonth but i'm having an error. Do you know how will i fix that? Thank you very much.
Shahan Ayyub 12-Oct-12 1:31am
What error did you get ?
Have you tried something like this :

Dim daysInMonth As Integer = DateTime.DaysInMonth(DateTimePicker1.Value.Year, DateTimePicker1.Value.Month)

It will print the days in month from the date chosen from datetimepicker1 control.
Janna Dela Cruz 12-Oct-12 1:49am
Actually, that's what I'm using.

This is the error "ArgumentOutOfRangeException was unhandled : Year, Month and Day parameters describe an un-representable DateTime"

I think it has something to do with the number of days in a month. Because the error comes whenever i used months that has 31 days, as well as the 28/29.
Shahan Ayyub 12-Oct-12 2:03am
I have just re-checked the code it seems to working fine. Can you post your code ? Also, if you could provide few more lines other than the affected line. It would be great to troubleshoot the issue. Also, Can you tell us which date have you selected ?
Janna Dela Cruz 12-Oct-12 2:18am
Here is the code
Public Sub SetDate2()
Dim Mon As Integer = DateTimePicker1.Value.Month
Dim Yr As Integer = DateTimePicker1.Value.Year
Dim Days As Integer = System.DateTime.DaysInMonth(Yr, Mon)


DateTimePicker2.MinDate = New DateTime(Yr, Mon, Days)
DateTimePicker2.MaxDate = DateTimePicker2.MinDate.AddMonths(5)

Catch ex As Exception
End Try
End Sub

I chose January in DateTimePicker1 then when i tried to choose February in the DateTimePicker2 the error comes out.
Shahan Ayyub 12-Oct-12 3:17am
The code I suggested will take the whole month, say you have selected 25 jan 2012 in datetimepicker1 so datetimepicker2 will start from 1 jan 2012. Is it the desired case ?

My understanding is that the error should come here:

DateTimePicker2.MinDate = New DateTime(Yr, Mon, Days)

because "Days" contains the number of days in a month of a particular year. So if its a January it will be 31 and do not exists in February. Have you tried changing this to:

DateTimePicker2.MinDate = New DateTime(Yr, Mon, 1)

That is, take the month from start as mindate.
Janna Dela Cruz 12-Oct-12 3:23am
Yeah I tried it, but it will only print the 1st day. Example, if i choose January to February it will only print January 1 - 31 and February 1. The output should be January 1 - 31 and February 1 - 28/29. How will i fix that? By the way I'm using UpDown of DateTimePicker.
Shahan Ayyub 12-Oct-12 6:21am
What about now:

DateTimePicker2.MaxDate = DateTimePicker.MaximumDateTime ' reset range before setting
DateTimePicker2.MinDate = DateTimePicker1.Value
DateTimePicker2.MaxDate = DateTimePicker2.MinDate.AddMonths(5)
Shahan Ayyub 12-Oct-12 18:17pm
Any updates ?
Janna Dela Cruz 16-Oct-12 20:56pm
Sorry late reply. I didn't get a chance to go online for the past days. It's done. Thanks a lot. Your a great help. :)
Shahan Ayyub 17-Oct-12 1:37am
glad to see your issue resolved! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100