對於您的問題的第一部分,我喜歡date.utils庫中的relativedelta。
from datetime import date
from dateutil.relativedelta import relativedelta, TH
def get_thurs(dt):
print (dt + relativedelta(day=31, weekday=TH(-1)))
get_thurs(date(2016, 7, 7))
get_thurs(date(2019, 6, 4))
2016-07-28
2019-06-27
對於第二部分,我想你想的東西有點像這樣:
def get_thurs(dt):
return (dt + relativedelta(day=31, weekday=TH(-1)))
def later_date(dt):
if dt.month == date.today().month: # checks if in current month
if get_thurs(dt).day > dt.day: # checks if last Thurs has passed
raise Exception
else:
print "It is before that Thursday"
later_date(date(2016, 7, 23))
later_date(date(2016, 7, 29))
It is before that Thursday
Traceback (most recent call last):
File "calculator/bw_events_cookies/tests/ideas.py", line 31, in <module>
later_date(date(2016, 7, 23))
File "calculator/bw_events_cookies/tests/ideas.py", line 26, in later_date
raise Exception
Exception
我有不知道你在說什麼。你能詳細解釋一下你的問題嗎? – refi64