2nd and 4th Saturday of the month

All Sample Codes Date

By default, current month is used , you can change the below code to any month and year.
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

dt=date(year,month,1)    # first day of month
first_w=dt.isoweekday()  # weekday of 1st day of the month
if(first_w==7): # if it is Sunday return 0 
	first_w=0
saturday2=14-first_w
dt=date(year,month,saturday2)
print(dt.day,dt.month,dt.year)
To display 4th Satruday, add this code at the end.
saturday4=28-first_w
dt=date(year,month,saturday4)
print(dt.day,dt.month,dt.year)

All months 2nd and 4th Saturday of a year

Change the year to get month wise 2nd and 4th Saturday.
year=2021 # change the year 
for month in range(1,13):
    dt=date(year,month,1)    # first day of month
    first_w=dt.isoweekday()  # weekday of 1st day of the month
    if(first_w==7): # if it is Sunday return 0 
        first_w=0
    saturday2=14-first_w
    dt1=date(year,month,saturday2)
    saturday4=28-first_w  # 4th Saturday 
    dt2=date(year,month,saturday4)
    print(dt2.strftime('%Y-%B : '),dt1.day,' and ', dt2.day)
Output
2021-January :  9  and  23
2021-February :  13  and  27
2021-March :  13  and  27
2021-April :  10  and  24
2021-May :  8  and  22
2021-June :  12  and  26
2021-July :  10  and  24
2021-August :  14  and  28
2021-September :  11  and  25
2021-October :  9  and  23
2021-November :  13  and  27
2021-December :  11  and  25
For the year 2022 output
2022-January :  8  and  22
2022-February :  12  and  26
2022-March :  12  and  26   
2022-April :  9  and  23    
2022-May :  14  and  28
2022-June :  11  and  25
2022-July :  9  and  23
2022-August :  13  and  27
2022-September :  10  and  24
2022-October :  8  and  22
2022-November :  12  and  26
2022-December :  10  and  24
All Date Objects Date & Time Exercises
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    09-04-2021

    we are getting error when the date is 2018/07/28

    17-04-2021

    No need to enter full date, just set the year value to 2018 and month to 7 , it will return the 2nd Saturday ..

    19-08-2021

    Doesn't work when a month begins on Sunday

    04-09-2021

    For Sunday the value is 0 , so changed to 7 by adding if condition. Thanks for pointing this.

    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