Sample Codes using Python Date functions

Display today's,yesterday, & tomorrow

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

Display next five days date

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

Check if current year is a leap year

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)

List all Saturday & Sundays of a Period

Accept start date and end date as inputs, list out all the Saturdays and sundays falling within this date range.
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

Weekday from input date

Ask the user to input any date using the given format. Display the weekday of the user input date.
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

Display the date for the first Monday of current month

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)

Adding Time

Print time by adding 2 hours and 35 mintues to current time.
import datetime
tm = datetime.datetime.now() + datetime.timedelta(minutes=35,hours=2)
print(tm.strftime('%H:%M:%S')) # 13:33:37

Findout first day of next month

We can add 1 to current month and then use 1 as day to create the date object but this will return value error for the month of december. So in place of month we will use dt.month%12, this will return 0 for december month.
Similarly for the year part, only for December month the year part is added by 1 and for rest of the month 0 is added to current year. For year we used 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 )

Last day of current month

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
Date All timedelta objects relativedelta
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer