Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Determine Last Friday's Date using Python?
In this article, we will learn how to determine last Friday's date using Python. This is useful for scheduling, reporting, and date-based calculations in business applications.
Methods Used
The following are the various methods to accomplish this task
Using datetime module
Using dateutil package
Method 1: Using Datetime Module
This approach uses Python's built-in datetime module to calculate the previous Friday by determining the current weekday and counting backwards.
Algorithm
Following are the steps to perform this task
Import
datetimeandtimedeltafrom datetime moduleCreate a list of weekday names for reference
Define a function to find the previous occurrence of any given day
Calculate the difference between current day and target day
Subtract the appropriate number of days to get the previous date
Example
The following program returns the last Friday's date using datetime module
# importing datetime, timedelta from datetime module
from datetime import datetime, timedelta
# list of days in a week
weekdays_list = ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday', 'Sunday']
# creating a function to return the last weekday of input day
def get_previous_weekday(input_day, start_date=None):
# check whether the start date is None
if start_date is None:
# assigning today's date(current date) to the start date
start_date = datetime.today()
# getting the current weekday number (0=Monday, 6=Sunday)
current_day_number = start_date.weekday()
# get the index of the target weekday
target_day_number = weekdays_list.index(input_day)
# calculate how many days ago the target day occurred
days_ago = (7 + current_day_number - target_day_number) % 7
# if target day is today, go back to previous week
if days_ago == 0:
days_ago = 7
# subtract the number of days to get the target date
target_date = start_date - timedelta(days=days_ago)
return target_date
# printing today's current date and time
print("Current date and time:", datetime.today())
# printing the last Friday date
print("Last Friday Date:", get_previous_weekday('Friday'))
# printing other previous weekdays
print("Last Monday Date:", get_previous_weekday('Monday'))
print("Last Wednesday Date:", get_previous_weekday('Wednesday'))
The output of the above code is
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 dateutil package provides more sophisticated date manipulation capabilities. It uses relativedelta which supports operations with years, months, weeks, and specific weekdays.
The dateutil.relativedelta.relativedelta adds support for periods defined in terms of years, months, weeks, or days, whereas timedelta only supports days and weeks.
Example
The following program returns 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 FR
# taking the start date as the current date
start_date = datetime.now()
# printing today's 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)))
The output of the above code is
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
Comparison
| Method | Dependencies | Complexity | Best For |
|---|---|---|---|
| datetime module | Built-in | Manual calculation | Simple date arithmetic |
| dateutil package | External package | Simple syntax | Complex date operations |
Conclusion
Both methods effectively determine last Friday's date. Use the datetime module for simple cases without external dependencies, or dateutil for more complex date calculations with cleaner syntax.
