How to Determine Last Friday’s Date using Python?


In this article, we will learn how to determine last Friday’s Date using Python.

Methods Used

The following are the various methods to accomplish this task −

  • Using datetime module

  • Using dateutil package

Method 1: Using Datetime Module

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Use the import keyword to import the datetime, timedelta from datetime module.

  • Create a variable to store the list of all the days in a week(weekdays).

  • Create a function getPreviousByDay() to return the last weekday of the input day by accepting the input day, start_date=None as arguments

  • Use the if conditional statement to check whether the start date is None.

  • Assign today's date(current date) to the start date.

  • Get the current weekday number using the weekday() function(returns an integer corresponding to the given day of the week) of datetime module.

  • Get the index of the target weekday(given input day) using the index() function.

  • Get the number of days from the last week.

  • Use the if conditional statement to check whether the above days ago variable result is equal to 0 then Assign 7 to the days ago variable.

  • Else Subtract the above number of days from the current date(start date) to get the last week's date on the given day

  • Return the last week's date of the input date.

  • Print today's/current date and time using the today() function of datetime.

  • Print the last Friday date by calling the above-defined getPreviousByDay() function by passing inputDay as 'Friday’ as an argument.

  • In the same way print the last Monday,Wednesday date by passing the input date as ‘Monday’ and ‘Wednesday’ to getPreviousByDay() function.

Example

The following program returns the Determine Last Friday’s/anyday Date using datetime module

# importing datetime, timedelta from datetime module
from datetime import datetime, timedelta
# list of days in a week
weekdaysList = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                'Friday', 'Saturday', 'Sunday']
# creating a function to return the last weekday of input day by
# accepting the input day, start_date=None as arguments
def getPreviousByDay(inputDay, start_date=None):
   # check whether the start date is None
      if start_date is None:
         # assiging today's date(current date) to the start date
         start_date = datetime.today()
         # getting the weekday number
         dayNumber = start_date.weekday()
         # Get the index of the target weekday
         dayNumberTarget = weekdaysList.index(inputDay)
         # getting the last weekday
         daysAgo = (7 + dayNumber - dayNumberTarget) % 7
      # checking whether the above days ago result is equal to 0
      if daysAgo == 0:
         # assigning 7 to the days ago
         daysAgo = 7
      # Subtract the above number of days from the current date(start date)
      # to get the last week's date of the given day
      targetDate = start_date - timedelta(days=daysAgo)
      # returning the last week's date of the input date
      return targetDate

# printing today's/current date and time
print("Current date and time:", datetime.today())
# printing the last Friday date by calling the above defined
# getPreviousByDay() function by passing inputDay as 'Friday' as an argument
print("Last Friday Date:", getPreviousByDay('Friday'))
# printing the last Monday date
print("Last Monday Date:", getPreviousByDay('Monday'))
# printing the last Wednesday date
print("Last Wednesday Date:", getPreviousByDay('Wednesday'))

Output

On executing, the above program will generate the following output −

Current date and time: 2023-01-01 08:15:35.935826
Last Friday Date: 2022-12-30 08:15:35.936020
Last Monday Date: 2022-12-26 08:15:35.936153
Last Wednesday Date: 2022-12-28 08:15:35.936264

Method 2: Using Dateutil Package

The start date and the target date are mapped to their corresponding numeric positions in the week in this code (with Monday as day 0). Then, to determine how many days ago since the target date last occurred, modular arithmetic is employed.

The target date is then determined by subtracting the start date from the appropriate timedelta instance.

Installing the python-dateutil package may be preferable if you frequently run date calculations like this.

Here is an example of the same calculation being made using dateutil's relativedelta() function.

The dateutil.relativedelta.relativedelta adds support for periods defined in terms of years, months, weeks, or days as well as the ability to define absolute values for year, month, or day, whereas the timedelta only supports days (and weeks).

Example

The following program returns the Determine Last Friday’s and Next Friday’s Date using dateutil module

# importing datetime from datetime module
from datetime import datetime
# importing relativedelta from dateutil module
from dateutil.relativedelta import relativedelta
from dateutil.rrule import *
# taking the start date as the current date
start_date = datetime.now()
# printing the todays/current date and time
print("Current Date:", start_date)
# printing the Next Friday date
print("Next Friday Date:", start_date + relativedelta(weekday=FR))
# printing the Last Friday date
print("Last Friday Date:", start_date + relativedelta(weekday=FR(-1)))

Output

On executing, the above program will generate the following output −

Current Date: 2023-01-15 08:19:49.267666
Next Friday Date: 2023-01-20 08:19:49.267666
Last Friday Date: 2023-01-13 08:19:49.267666

Conclusion

In this article, we learned how to use two different methods to get the date from the last Friday in Python. We also put into practice how to acquire the last weekday of the given input day as well as how to retrieve the next weekday of the given day.

Updated on: 24-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements