如何检查今天是否是Python中的星期一

时间:2017-08-24 21:01:43

标签: python datetime

我如何撰写一份声明:

<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.

但我知道这不是正确的做法。我该怎么做呢?

4 个答案:

答案 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.datedatetime.datetime 对象具有today方法,该方法分别返回DateDatetime对象。

两者都有weekdayisoweekday方法。 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

请参阅documentation

答案 3 :(得分:-1)

from datetime import datetime

# Print True just when is Monday.
print(datetime.now().weekday() == 0)