Created
January 31, 2015 12:41
-
-
Save sharoonthomas/6e54065bafd57e6a4c51 to your computer and use it in GitHub Desktop.
Finding monday of a week
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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