首页 > 解决方案 > 在两个日期之间执行函数 X 次,我该怎么做?

问题描述

如果我能够在两个日期之间的(用户指定的)时间内执行一个函数,我会怎么做?

我在正确的轨道上吗?

from datetime import date, datetime
from upload import *

current_time = datetime.utcnow()
start_time = datetime.time.hour(17)
end_time = datetime.time.hour(20)

imageposts = []
post_limit = 0

for imagepost in imageposts:
    if start_time <= current_time & current.time <= end_time & post_limit <= 3:
        try:
            upload()
            postlimit += 1
        except:
            print('Current time is not between times')

标签: pythondatetimefor-loopif-statement

解决方案


尝试这个:

from datetime import datetime
from upload import *

current_time = datetime.utcnow()
year, month, day = current_time.year, current_time.month, current_time.day
start_time = datetime(year, month, day, 17)
end_time = datetime(year, month, day, 20, 59, 59)

imageposts = []
post_limit = 0

for imagepost in imageposts:
    if start_time <= current.time <= end_time and post_limit <= 3:
        try:
            upload()
            post_limit += 1
        except:
            print('Current time is not between times')


推荐阅读