I' developing a django project and in my template user can select a day.As shown as bottom picture.
And after day select post request is working.And in my views.py I can get the day name.Like(Monday,Tuesday,...).Using this knowledge, I want to get date of this week.For example coming value of request is Wednesday and I want to get what date is Wednesday in this week like (03.11.2021)
CodePudding user response:
create a file called utils.py and import it inside your views.py and your can pass it string like 'Sunday' it will give the correct date and make sure the first later is capitalyze by using .upper()
before passing to the function.This is just an idea you can make it better ).
from datetime import date,timedelta
import calendar
def find_day(week_day):
my_date = date.today()
a = calendar.day_name[my_date.weekday()]
weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
now = weekdays.index(a)
later = weekdays.index(week_day)
if later > now:
date = my_date - timedelta(days= now - later)
else:
date = my_date - timedelta(days= now - later)
return date
CodePudding user response:
I assume you have a certain data (e.g. today) and you want to know the date of another day of the week within the same week.
You could use datetime
and time
, to get the weekday as integer of today, compute the difference in days between the weekday you have and the one you target, and finally apply that difference to your original datetime
.
The weekday is optionally converted to an integer using strptime()
and trying all known weekday format strings. Do note that tm_wday
as well as weekday()
use the same convention for "Monday" -> 0 through "Sunday" -> 6, while %w
and %u
format codes use different conventions. It is crucial to compute the difference with integer weekdays obtained following the same convention. Hence, if weekday
is given as an integer, the "Monday" -> 0 through "Sunday" -> 6 convention should be used.
import datetime
import time
def weekday_as_int(weekday):
weekday = weekday.strip()
result = None
# %a -- Weekday as locale’s abbreviated name.
# %A -- Weekday as locale’s full name.
# %w -- Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
# %u -- ISO 8601 weekday as a decimal number where 1 is Monday and 7 is Sunday
for fmt in ("%a", "%A", "%w", "%u"):
try:
result = time.strptime(weekday, fmt).tm_wday
except ValueError:
pass
if result is not None:
return result
if not result:
raise ValueError(f'Could not parse `{weekday}` as week day')
def get_date_for_weekday(curr_date, weekday):
if not isinstance(weekday, int):
weekday = weekday_as_int(weekday)
curr_weekday = curr_date.weekday()
delta = weekday - curr_weekday
return curr_date datetime.timedelta(days=delta)
# datetime.date(2021, 11, 5) is a Friday
get_date_for_weekday(datetime.date(2021, 11, 5), "Monday")
# datetime.date(2021, 11, 1)
get_date_for_weekday(datetime.date(2021, 11, 5), "Friday")
# datetime.date(2021, 11, 5)
get_date_for_weekday(datetime.date(2021, 11, 2), "wed")
# datetime.date(2021, 11, 3)