![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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() function
Using relativedelta() function to get Last Monday
Using relativedelta() function to get next Monday
Using relativedelta() function to get next nth Monday
Using timedelta() function to get previous nth Monday
Method 1: Using timedelta
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the, datetime (To work with Python dates and times) module.
Use the today() function (gets the current local date), to get today’s current local date and print it.
Increment the Today’s date with 1 week by passing the weeks as 1 as an argument to the timedelta() function.
timedelta()- Python's timedelta() function is available in the datetime library and is typically used to calculate differences between dates. It can also be used to manipulate dates in Python, and this function makes it very simple for users to do so.
Syntax
Datetime.timedelta(days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0)
Print the next Monday date.
Example
The following program returns the next Monday date using the datetime.timedelta() function−
# importing datetime module import datetime # getting today's date todayDate = datetime.date.today() print('Today Date:',todayDate) # Increment today's date with 1 week to get the next Monday nextMonday = todayDate + datetime.timedelta(days=-todayDate.weekday(), weeks=1) print('Next Monday Date:',nextMonday)
Output
On executing, the above program will generate the following output −
Today Date: 2022-09-07 Next Monday Date: 2022-09-12
Method 2: Using relativedelta() function to get last Monday
The timedelta only supports days (and weeks), whereas relativedelta allows years, months, weeks, or days, as well as providing absolute values for year, month, or day.
A relativedelta has a far larger number of parameters than a timedelta.
relativedelta.relativedelta(self, dt1=None, dt2=None, years=0, months=0, days=0, leapdays=0, weeks=0, hours=0, minutes=0, seconds=0, microseconds=0, year=None, month=None, day=None, weekday=None, yearday=None, nlyearday=None, hour=None, minute=None, second=None, microsecond=None)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the datetime (To work with dates and times) module.
Use the import keyword, to import the relativedelta, MO from the dateutil module.
Use the today() function (gets the current local date), to get today’s current local date and create a variable to store it.
Pass the weekday as an argument to the relativedelta() function. Here MO(-1) says getting last week Monday, MO means Monday.
Print the last Monday date.
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 todayDate = date.today() print('Today Date:',todayDate) # Pass MO(-1) as an argument to relativedelta to set weekday as Monday and -1 signifies last week's Monday lastMonday = todayDate + relativedelta(weekday=MO(-1)) # printing the last Monday date print("The last Monday date:", lastMonday)
Output
Today Date: 2022-09-07 The last Monday date: 2022-09-05
Method 3: Using relativedelta() function to get next Monday
It is similar to the previous method, but this time we pass the argument MO(1) to get the next Monday's date.
Example
# 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 todayDate = date.today() print('Today Date:',todayDate) # Pass MO(1) as an argument to relativedelta to set weekday as Monday and 1 signifies next week's Monday nextMonday = todayDate + relativedelta(weekday=MO(1)) # printing the Next Monday date print("The Next Monday date:", nextMonday)
Output
Today Date: 2022-09-07 The Next Monday date: 2022-09-12
Method 4: Using relativedelta() function to get next nth Monday
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Give the value of n and store it in a variable.
Pass the argument MO(n) to the relativedelta() function to get the next nth Monday.
Print the value for next nth Monday.
Example
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 todayDate = date.today() print('Today Date:',todayDate) # Pass MO(n) as an argument to relativedelta to set weekday as Monday and n signifies nth week's Monday nextNthMonday = todayDate + relativedelta(weekday=MO(n)) # printing the given nth Monday date print(n,"th Monday date from today is:",nextNthMonday)
Output
Today Date: 2022-09-07 4 th Monday date from today is: 2022-10-03
Method 5: Using timedelta() function to get previous nth Monday
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Give the value of n and store it in a variable.
Decrement the Today’s date with n weeks Monday by passing the weeks as n as an argument to the timedelta() function.
Print the value for the previous nth Monday.
Example
# importing datetime module import datetime # getting today's date todayDate = datetime.date.today() # Given n n = 4 print('Today Date:',todayDate) # Decrement today's date with n weeks to get the previous nth Monday previousNthMonday = todayDate - datetime.timedelta(days=-todayDate.weekday(), weeks=n) # printing the given nth Monday date print("Previous",n,"th Monday date from today is:",previousNthMonday)
Output
Today Date: 2022-09-07 Previous 4 th Monday date from today is: 2022-08-12
Conclusion
In this article, we learned how to find the Monday date using five different examples, as well as how to find the next and previous Mondays.