Finding Date of Last Saturday of current Month Using Python

Here we are going to find date of last saturday of current month using python

Date Of Last Saturday Of Current Month Using Python

The Date Of Last Saturday of Current Month Using Python Programming. Here we are using some of the Inbuilt  python Collection Of Tools  to write this Code.

  • Calendar module

Here We use  Calendar module provides a wide range of functionalities for working with dates.

  • Datetime module

Here We use Datetime module in Python provides classes for manipulating dates and time.

  • For Loop

Here We Use For  loop in Python combined with the range() function, is a powerful tool for iterating over a sequence of numbers. If you want to iterate over a range of days, you can use the range() function to specify the start, stop, and optionally.

  • IF Condition

Here We Use  IF statement in Python is used for conditional execution of code. It allows you to execute certain blocks of code only if a specified condition is true.

Here  we are Going to start the Code

We are starting with the Calendar Module and the Datetime Module in  The Code

import calendar
import datetime

def last_saturday_of_month(year, month):

Here We Get the number of days in the month

num_days = calendar.monthrange(year, month)[1]

Here We Are Using For Loop and IF Condition the range functions()  Iterate from the last day of the month to find the last Saturday

for day in range(num_days, 0, -1):
        date = datetime.date(year, month, day)
        if date.weekday() == 5:  # 5 corresponds to Saturday
            return date

Here We Are Get the current year and month to find the last Saturday of Current Month

current_date = datetime.date.today()
current_year = current_date.year
current_month = current_date.month

 

Here we are finally Find the date of the last Saturday of the current month
last_saturday = last_saturday_of_month(current_year, current_month)
print("Last Saturday of the current month:", last_saturday)
output:
Last Saturday of the current month: 2024-05-25

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top