我如何撰写一份声明:
<div style="background-color:blue; position:absolute; top:0px; left:0px; z-index:0;">
</div>
<div style="background-color:green;position:relative; transform:translateX(0); z-index:2;">
</div>
<div style="background-color:red; position:absolute; top:20px; left:20px; z-index:1;">
</div>
我的想法是:
If today is Monday, then run this function.
但我知道这不是正确的做法。我该怎么做呢?
答案 0 :(得分:9)
您可以像这样使用date.weekday()
:
from datetime import date
if date.today().weekday() == 0:
run_report()
答案 1 :(得分:6)
import datetime as dt
dt.date.today().isoweekday() == 1 # 1 = Monday, 2 = Tues, etc.
答案 2 :(得分:1)
datetime.date
和datetime.datetime
对象具有today
方法,该方法分别返回Date
和Datetime
对象。
两者都有weekday
和isoweekday
方法。
weekday
从星期一开始计算= 0,而isoweekday
从星期一开始计算= 1:
from datetime import date, datetime
if date.today().weekday() == 0:
# it is Monday
if datetime.today().isoweekday() == 1:
# it is Monday
答案 3 :(得分:-1)
from datetime import datetime
# Print True just when is Monday.
print(datetime.now().weekday() == 0)