时间判断比较

1
2
3
4
5
6
7
8
9
10
11
12
13
import datetime

b_time = datetime.datetime.now().strftime('%H:%M')
e_time = datetime.datetime.strptime("03:00", '%H:%M')
s_time = datetime.datetime.strptime("03:13", '%H:%M')


begin_datime = datetime.datetime.strptime(str(b_time), "%H:%M")
end_datime = datetime.datetime.strptime(str(e_time)[11:16], "%H:%M")
s_datime = datetime.datetime.strptime(str(s_time)[11:16], "%H:%M")

if end_datime >= begin_datime and begin_datime <= s_datime:
print("111")

判断是否为周六日

1
2
3
sunday = datetime.datetime.utcnow().weekday()
if sunday == 5 or sunday == 6:
print("000")

日期和天数 提前和延后

pip install arrow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 月份前两个月
def date_months_2(datime):
# datime = "2021-10-25"
a, b, c = datime.split('-')
# 将日期做分割给三个变量
now = arrow.get(int(a), int(b), int(c))
# months 月份 加减来改参
last_month = str(now.shift(months=-2))[:10]
return last_month


# 月份前一个月
def date_months_1(datime):
a, b, c = datime.split('-')
now = arrow.get(int(a), int(b), int(c))
last_month = str(now.shift(months=-1))[:10]
return last_month


# 天数加一天
def date_days(value):
date = date_months_2(value)
a, b, c = date.split('-')
now = arrow.get(int(a), int(b), int(c))
# 天数加减参数
last_month = str(now.shift(days=+1))[:10]
return last_month

任务调度

pip install schedule

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import time
import datetime
import schedule


def job():
print("I'm working^", datetime.datetime.now())


# 每隔三秒执行
schedule.every(3).seconds.do(job)
# 每隔三分钟执行
schedule.every(3).minutes.do(job)
# 每隔三小时执行
schedule.every(3).hours.do(job)
# 每隔三天执行
schedule.every(3).days.do(job)
# 每隔三周执行
schedule.every(3).weeks.do(job)
# 每分钟第23秒执行
schedule.every().minute.at(":23").do(job)
# 每小时第42分钟执行
schedule.every().hour.at(":42").do(job)
# 每5小时20分钟30秒执行
schedule.every(5).hours.at("20:30").do(job)
# 每天10点30执行
schedule.every().day.at("10:30").do(job)
schedule.every().day.at("10:30:42").do(job)

# 每周三15.38执行
schedule.every().wednesday.at("15:38").do(job)

while 1:
schedule.run_pending()
time.sleep(1)