Computer >> Computer tutorials >  >> Programming >> Python

How to find only Monday's date with Python?


You can find the next Monday's date easily with Python's datetime library and the timedelta object. You just need to take today's date. Then subtract the number of days which already passed this week (this gets you 'last' monday). Finally add one week to this date using a timedelta object and voila you get the next monday's date.

Example

import datetime
today = datetime.date.today()
next_monday = today + datetime.timedelta(days=-today.weekday(), weeks=1)
print(next_monday)

Output

This will give the output −

2018-01-08