在python中使用datetime检查分钟值

时间:2013-03-17 13:57:54

标签: python datetime

假设我有一个start_date

start_date = datetime.time(7, 55, 56)

我创建了一个时间列表:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 5):
        start = datetime.time(y, x)
        timelist.append(start)

timelist = [datetime.time(0, 0), 
            datetime.time(0, 10), 
            datetime.time(0, 20), 
            datetime.time(0, 30), 
            datetime.time(0, 40), 
            datetime.time(0, 50), 
            datetime.time(1, 0),
            ...]

x.hour返回[0, 1, 2, ..., 23] x.minute返回[0, 10, 20, 30, 40, 50]

然后我要创建一个字典{ [time: 00:00:00, value: 0], [time: 00:10:00, value=0], ... }

现在时间是x。并通过检查开始时间是否为:

来创建值

例如:

start_date = 07:55:56 → at 07:50:00 value = 1
start_date = 08:03:22 → at 08:00:00 value = 1
start_date = 08:18:00 → at 08:10:00 value = 1

否则值为0

我需要帮助创建用于检查分钟的if语句。

到目前为止,我有:

for x in timelist:
    if x.hour == start_date.hour:
        ...

1 个答案:

答案 0 :(得分:1)

有点难以理解的是什么。

您想知道start_date属于哪个时间间隔吗?

如果是这样的话:

timelist = []
for y in range(0, 24, 1):
    for x in range(0, 60, 10):     
        # should be ten? 
        start = datetime.time(y, x)
        timelist.append(start)


timedict = {}
for start in timelist:
    timedict[start] = 0
# is it the dictionary looking for?

start_date = datetime.time(7, 55, 56)

for i in range(len(timelist)):
    if timelist[i] <= start_date:
        if i == len(timelist)-1 or start_date < timelist[i+1]
            timedict[timelist[i]] += 1  # or = 1, depend on your usage