Calculate the week from current datetime/date:
Just to keep it shorter.
import datetime year, week, day = datetime.datetime.now().isocalendar() # weekday is from 1 - 7 # converting back to datetime/date the_date = datetime.date.fromisocalendar(year, week, day) the_datetime = datetime.datetime.fromisocalendar(year, week, day)Now getting for date X the start date of the week and the end date of the week:
def get_week(date_or_datetime): MONDAY, SUNDAY = 1, 7 calendar = datetime.date.fromisocalendar year, week, _ = date_or_datetime.isocalendar() return calendar(year, week, MONDAY), calendar(year, week, SUNDAY)I assigned inside the function
datetime.date.fromisocalendar
to calendar
.Just to keep it shorter.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!