# tkcalendar mindate & maxdate options

Tkinter tkcalendar
In our calendar we can use the option mindate and maxdate to disable any dates before mindate and after maxdate.
Here from 3rd Aug 2021 ( minimum date ) to 25th Aug 2021 ( maximum date ) are available for selection. ``````import tkinter  as tk
from tkinter import ttk
from tkcalendar import Calendar
from  datetime import date
my_w = tk.Tk()
my_w.geometry("380x220")
dt = date.today() # today
dt1=date(2021,8,3) # specific date Year, month , day
dt2=date(2021,8,25) # Maximum date in Year, month , day

cal=Calendar(my_w,selectmode='day',mindate=dt1,maxdate=dt2)

my_w.mainloop()``````

## Today's Date with mindate & maxdate

We can allow to select all future dates including today by using mindate option.
``````import tkinter  as tk
from tkcalendar import Calendar
from datetime import datetime
my_w = tk.Tk()
my_w.geometry("320x210")
date_today = datetime.now() # today's date

cal=Calendar(my_w,selectmode='day', mindate=date_today)

my_w.mainloop()``````
We can disable selection of all future dates by using maxdate option
``cal=Calendar(my_w,selectmode='day', maxdate=date_today)``

## Using timedelta

Select all days till tomorrow ( use timedelta )
``````from datetime import timedelta,date,datetime
dt = date.today() + timedelta(days=1) # Tomorrow
cal=Calendar(my_w,selectmode='day',maxdate=dt)``````
Select all days starting from yesterday
``````dt = date.today() + timedelta(days=-1) # Yesterday
cal=Calendar(my_w,selectmode='day',mindate=dt)``````

## Using relativedelta

The variable dt can be used to set mindate or maxdate based on requirment
``````from dateutil.relativedelta import relativedelta
from datetime import date
dt = date.today() + relativedelta(days=1))# tomorrow  ``````
Check how future dates are enabled using relativedelta.
``````import tkinter  as tk
from tkcalendar import Calendar
from dateutil.relativedelta import relativedelta
from datetime import date
my_w = tk.Tk()
my_w.geometry("320x210")
dt = date.today() + relativedelta(years=1,months=2,days=3)
cal=Calendar(my_w,selectmode='day',maxdate=dt,mindate=date.today())

my_w.mainloop()``````

## Dynamic minimum date and maximum date

Once the user clicks a date, it became minimum date ( or start date ) and 10 days after this date became the maximum date ( or end date ). Read more about timedelta() and other data parameters to add or subtract.

Add one Reset button to restore the initial stage so user can select again.
``````import tkinter  as tk
from tkinter import ttk
from tkcalendar import Calendar
from datetime import timedelta,date,datetime
my_w = tk.Tk()
my_w.geometry("380x220")
sel=tk.StringVar()

def my_upd(*args): # triggered when value of string varaible changes
dt1=cal.selection_get() # minmum date
dt2=dt1 + timedelta(days=10) # maximum date, added 10 days
cal.config(mindate=dt1) # set minimum date
cal.config(maxdate=dt2) # set maximum date
cal=Calendar(my_w,selectmode='day',textvariable=sel)

b1=tk.Button(my_w,text='Reset'
,command=lambda:cal.config(mindate=None,maxdate=None))
b1.grid(row=2,column=1)

sel.trace('w',my_upd) # on change of string variable
my_w.mainloop()``````

## Example

This is an example only to understand how to set the maximum and minimum dates.

Based on the date for first dose of vaccination, the range of dates available for second dose is decided. Here after 10 days of first does, the eligibility of 2nd does is started. But 2nd does is be taken within 15 days of eligibility.

Changes in the above script is here.
``````days_gap=10 #Minimum gap to be maintained
end_date=15 #Within this range 2nd does to be taken
def my_upd(*args): # triggered when value of string varaible changes
dt1=cal.selection_get() + timedelta(days=days_gap) # Start for 2nd dose
dt2=dt1 + timedelta(days=end_date) # end date for 2nd dose
cal.config(mindate=dt1) # set minimum date
cal.config(maxdate=dt2) # set maximum date ``````
Subscribe to our YouTube Channel here

## Subscribe

* indicates required
Subscribe to plus2net plus2net.com
{"ft_lang_label":"__label__en","ft_lang_prob":0.5474728,"math_prob":0.9128371,"size":4035,"snap":"2022-27-2022-33","text_gpt3_token_len":1072,"char_repetition_ratio":0.15132721,"word_repetition_ratio":0.1414914,"special_character_ratio":0.25229245,"punctuation_ratio":0.13253012,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9889331,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-01T05:08:26Z\",\"WARC-Record-ID\":\"<urn:uuid:9d97177f-7e52-470a-9f1d-996c55d7a3fe>\",\"Content-Length\":\"23406\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ff643750-575c-4bd0-99b9-8c5f8124af74>\",\"WARC-Concurrent-To\":\"<urn:uuid:b73a1712-9e41-4da5-bab7-220f03481bd3>\",\"WARC-IP-Address\":\"166.62.28.120\",\"WARC-Target-URI\":\"https://www.plus2net.com/python/tkinter-tkcalendar-mindate-maxdate.php\",\"WARC-Payload-Digest\":\"sha1:4MEYP6HGEV3GLQHAWSEJBH4FCTIVT6S3\",\"WARC-Block-Digest\":\"sha1:5SPLZ62BNEVAL6OIISPYFGA5DGPBOYSY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103920118.49_warc_CC-MAIN-20220701034437-20220701064437-00322.warc.gz\"}"}