from datetime import date,timedelta
print("Today date : ",date.today()) # Today date : 2021-12-23
print("Today Month: ",date.today().month) # Today Month: 12
print("Yesterday : ",date.today() - timedelta(days=1)) # Yesterday : 2021-12-22
print("Tomorrow : ",date.today() + timedelta(days=1)) # Tomorrow : 2021-12-24
from datetime import date,timedelta
for i in range(5):
print(" Date : ",date.today() + timedelta(days=i))
Output
Date : 2021-12-29
Date : 2021-12-30
Date : 2021-12-31
Date : 2022-01-01
Date : 2022-01-02
from datetime import date
year=date.today().year
#year=2020
if(year%4==0):
print("This is a leap year : ",year)
else:
print("This year is not a leap year : ", year)
from datetime import datetime,timedelta
#start_date='1 Dec 2021' # date as string
#end_date='31 DEc 2021'
start_date=input("Enter start date: ")
end_date=input("Enter End date:")
start_date=datetime.strptime(start_date,'%d %b %Y') # match the format
end_date=datetime.strptime(end_date,'%d %b %Y')
day_count = (end_date - start_date).days + 1 # gap in Number of days
for dt in (start_date + timedelta(n) for n in range(day_count)):
if(dt.isoweekday()==7 or dt.isoweekday()==6 ): # Saturday & Sunday
print(dt.strftime('%Y-%m-%d (%A)'))
Output
Enter start date: 1 Dec 2021
Enter End date:25 Dec 2021
2021-12-04 (Saturday)
2021-12-05 (Sunday)
2021-12-11 (Saturday)
2021-12-12 (Sunday)
2021-12-18 (Saturday)
2021-12-19 (Sunday)
2021-12-25 (Saturday)
List of date and time formats is here from datetime import datetime
my_date1=input("Enter date dd/mm/YYYY: ")
my_date=datetime.strptime(my_date1,'%d/%m/%Y') # match the format
print(my_date1 ,my_date.strftime('%A'))
Output
Enter date dd/mm/YYYY: 31/12/2022
31/12/2022 Saturday
from datetime import date
year=date.today().year # change this to use year as input
month=date.today().month # change this to use month as input
#month=1
dt=date(year,month,1) # first day of month
first_w=dt.isoweekday() # weekday of 1st day of the month
if(first_w==1): # if it is Monday
monday1=first_w
else:
monday1=9-first_w
dt=date(year,month,monday1)
print(dt.day,dt.month,dt.year)
import datetime
tm = datetime.datetime.now() + datetime.timedelta(minutes=35,hours=2)
print(tm.strftime('%H:%M:%S')) # 13:33:37
dt.month%12, this will return 0 for december month.
dt.year + dt.month // 12
from datetime import date,timedelta
import datetime
#dt=date.today() # todays date
dt=datetime.datetime(2019,12,1) # YYYY,mm,dd format any valid date
dt2=datetime.datetime(dt.year + dt.month // 12,dt.month % 12 + 1, 1)
print("First day of next month :", dt2 )
dt2=datetime.datetime(dt.year + dt.month // 12,dt.month % 12 + 1, 1)-timedelta(days=1)
print("Last day of current month : " , dt2 )
Print 2nd and 4th Saturdays of any month

Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.