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 find only Monday\'s date with Python?
In this article, we will show you how to find only Monday's date using Python. We find the last Monday, next Monday, nth Monday's dates using different methods ?
Using
timedelta()functionUsing
relativedelta()function to get Last MondayUsing
relativedelta()function to get next MondayUsing
relativedelta()function to get next nth MondayUsing
timedelta()function to get previous nth Monday
Using timedelta() for Next Monday
The timedelta() function from Python's datetime library calculates differences between dates and can manipulate dates. It provides parameters for days, seconds, microseconds, milliseconds, minutes, hours, and weeks.
Syntax
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
Example
The following program returns the next Monday date using the datetime.timedelta() function ?
# importing datetime module
import datetime
# getting today's date
today_date = datetime.date.today()
print('Today Date:', today_date)
# Calculate next Monday by adjusting to Monday and adding 1 week
next_monday = today_date + datetime.timedelta(days=-today_date.weekday(), weeks=1)
print('Next Monday Date:', next_monday)
Today Date: 2022-09-07 Next Monday Date: 2022-09-12
Using relativedelta() for Last Monday
The relativedelta function supports years, months, weeks, and days, unlike timedelta which only supports days and weeks. It provides absolute values for year, month, or day along with weekday parameters.
Example
The following program returns the last Monday date using relativedelta() function ?
# importing date from datetime module
from datetime import date
# importing relativedelta, MO from dateutil
from dateutil.relativedelta import relativedelta, MO
# getting today's current local date
today_date = date.today()
print('Today Date:', today_date)
# Pass MO(-1) to get last Monday
last_monday = today_date + relativedelta(weekday=MO(-1))
# printing the last Monday date
print("The last Monday date:", last_monday)
Today Date: 2022-09-07 The last Monday date: 2022-09-05
Using relativedelta() for Next Monday
Similar to the previous method, but this time we pass MO(1) to get the next Monday's date ?
# importing date from datetime module
from datetime import date
# importing relativedelta, MO from dateutil
from dateutil.relativedelta import relativedelta, MO
# getting today's current local date
today_date = date.today()
print('Today Date:', today_date)
# Pass MO(1) to get next Monday
next_monday = today_date + relativedelta(weekday=MO(1))
# printing the Next Monday date
print("The Next Monday date:", next_monday)
Today Date: 2022-09-07 The Next Monday date: 2022-09-12
Using relativedelta() for Next nth Monday
To get the nth Monday from today, pass MO(n) where n is the number of Mondays ahead ?
from datetime import date
# importing relativedelta, MO from dateutil
from dateutil.relativedelta import relativedelta, MO
# Given n
n = 4
# getting today's current local date
today_date = date.today()
print('Today Date:', today_date)
# Pass MO(n) to get nth Monday ahead
next_nth_monday = today_date + relativedelta(weekday=MO(n))
# printing the given nth Monday date
print(n, "th Monday date from today is:", next_nth_monday)
Today Date: 2022-09-07 4 th Monday date from today is: 2022-10-03
Using timedelta() for Previous nth Monday
To get the previous nth Monday, subtract n weeks using timedelta() ?
# importing datetime module
import datetime
# getting today's date
today_date = datetime.date.today()
# Given n
n = 4
print('Today Date:', today_date)
# Go back n weeks from the most recent Monday
previous_nth_monday = today_date - datetime.timedelta(days=today_date.weekday(), weeks=n)
# printing the previous nth Monday date
print("Previous", n, "th Monday date from today is:", previous_nth_monday)
Today Date: 2022-09-07 Previous 4 th Monday date from today is: 2022-08-08
Comparison of Methods
| Method | Best For | Requires dateutil? |
|---|---|---|
timedelta() |
Simple date arithmetic | No |
relativedelta() |
Complex date calculations | Yes |
Conclusion
Use timedelta() for simple Monday calculations with built-in datetime module. Use relativedelta() for more readable code when working with specific weekdays and complex date operations.
