これは機能します:
import datetime as dt
dow={d:i for i,d in
enumerate('Mon,Tue,Wed,Thu,Fri,Sat,Sun'.split(','))}
def next_dow(d,day):
while d.weekday()!=day:
d+=dt.timedelta(1)
return d
d1=min(next_dow(dt.datetime(2013,4,1),day)
for day in (dow['Tue'],dow['Thu']))
d2=min(next_dow(dt.datetime(2013,4,2),day)
for day in (dow['Mon'],dow['Wed'],dow['Fri']))
for d in d1,d2:
print d.strftime('%Y-%m-%d')
または(おそらくより良いですが、一般的ではありません):
def next_dow(d,days):
while d.weekday() not in days:
d+=dt.timedelta(1)
return d
d1=next_dow(dt.datetime(2013,4,1),(dow['Tue'],dow['Thu']))
d2=next_dow(dt.datetime(2013,4,2),(dow['Mon'],dow['Wed'],dow['Fri']))
for d in d1,d2:
print d.strftime('%Y-%m-%d')
版画:
2013-04-02
2013-04-03