Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finding monday of a week
# Given a date find the monday and friday of the week
# This assumes that the week starts on sunday
import datetime
import calendar
from dateutil.relativedelta import relativedelta
def get_monday_in_week(date):
# Find the current week
current_week = date.isocalendar()[1]
delta = relativedelta(weeks=current_week-1, weekday=calendar.MONDAY)
# Take a reference which will always be on the first
# week of the year. Apparently the 4th of Jan is
# always on the first week ;-)
# proof ?
# >>> set([date(year, 1, 4).isocalendar()[1] for year in range(2000, 2999)])
# set([1])
return datetime.date(date.year, 1, 4) + delta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.