How to Set the Minimum And Maximum Dates at run time in MonthCalendar control

Some time users need to select date within some specific date-range, for it developer can set the minimum and maximum date for the MonthCalendar control such as if user want to select date between last month’s dates than developer should set the first date of the last month as min date of the Monthcalendar control and last date of the last month as maximum date this enables user to select date between the last month date ranges. In this article i am describing you how we can set the min date and max date for Month Calendar control.

MinDate property of the Month Calendar

The MinDate represent the minimum allowable date and default date value is 01/01/1753.

if you will set the Mindate earlier than 01/01/1753,the code will give an System.ArgumentException.

MaxDate property of the Month Calendar

The MaxDate represent the maximum allowable date and default date value is 12/31/9998.
if you will set the Maxdate greater than 12/31/9998,the code will give an System.ArgumentException.

Example:

  Private Sub Form3_Load(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
        MonthCalendar1.MinDate = #1/1/2012#
        MonthCalendar1.MaxDate = #6/30/2012#
    End Sub

Another Example to set the first date and last date of the previous month:

    Private Sub Form3_Load(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim firstDay_PrevMonth As DateTime
        Dim lastDay_PrevMonth As DateTime
        Dim Current_Month As New DateTime(DateTime.Today.Year, _
                             DateTime.Today.Month, 1)
 
        firstDay_PrevMonth = Current_Month.AddMonths(-1)
        lastDay_PrevMonth = Current_Month.AddDays(-1)
 
        MonthCalendar1.MinDate = firstDay_PrevMonth
        MonthCalendar1.MaxDate = lastDay_PrevMonth
    End Sub

 

Thanks