Python在开始时间到结束时间之间创建一个时间列表

时间:2017-03-30 06:53:56

标签: python python-2.7

我有start_dateend_datefrom timeend timeslot in min详细信息,我需要按照以下预期创建时间列表

import datetime

start_time = '9:00'
end_time = '18:00'
slot_time = 10

# Start date from today to next 5 day
start_date = datetime.datetime.now().date()
end_date = datetime.datetime.now().date() + datetime.timedelta(days=5)

预期输出( start_date end_date =总 5天

['9:00', '9:10', '9:20', '9:30', '9:40', '9:50', '10:00', '10:10', ..., '17.50', '18:00']
['9:00', '9:10', '9:20', '9:30', '9:40', '9:50', '10:00', '10:10', ..., '17.50', '18:00']
['9:00', '9:10', '9:20', '9:30', '9:40', '9:50', '10:00', '10:10', ..., '17.50', '18:00']
['9:00', '9:10', '9:20', '9:30', '9:40', '9:50', '10:00', '10:10', ..., '17.50', '18:00']
['9:00', '9:10', '9:20', '9:30', '9:40', '9:50', '10:00', '10:10', ..., '17.50', '18:00']

我的老虎机时间没有固定,根据插槽创建输出列表可以分别为10分钟,15分钟,20分钟,30分钟

如果我的老虎机时间= 15

预期输出( start_date end_date =总 5天

['9:15', '9:30', '9:45', '10:00', '10:15', ..., '17.45', '18:00']
['9:15', '9:30', '9:45', '10:00', '10:15', ..., '17.45', '18:00']
['9:15', '9:30', '9:45', '10:00', '10:15', ..., '17.45', '18:00']
['9:15', '9:30', '9:45', '10:00', '10:15', ..., '17.45', '18:00']
['9:15', '9:30', '9:45', '10:00', '10:15', ..., '17.45', '18:00']

2 个答案:

答案 0 :(得分:3)

您可以迭代想要的日子,然后在需要的时间内添加嵌套迭代。有关比较对象的可能性,您需要使用datetime函数将字符串转换为strptime个对象。最后,您可以使用.strftime对象的datetime方法以任意格式获取输出。

import datetime

start_time = '9:00'
end_time = '18:00'
slot_time = 10

# Start date from today to next 5 day
start_date = datetime.datetime.now().date()
end_date = datetime.datetime.now().date() + datetime.timedelta(days=5)

days = []
date = start_date
while date <= end_date:
    hours = []
    time = datetime.datetime.strptime(start_time, '%H:%M')
    end = datetime.datetime.strptime(end_time, '%H:%M')
    while time <= end:
        hours.append(time.strftime("%H:%M"))
        time += datetime.timedelta(minutes=slot_time)
    date += datetime.timedelta(days=1)
    days.append(hours)

for hours in days:
    print(hours)

答案 1 :(得分:0)

如果我正确理解你的问题,你可以这样解决:

from datetime import datetime,timedelta

def get_daily_slots(start, end, slot, date):
    # combine start time to respective day
    dt = datetime.combine(date, datetime.strptime(start,"%H:%M").time())
    slots = [dt]
    # increment current time by slot till the end time
    while (dt.time() < datetime.strptime(end,"%H:%M").time()):
        dt = dt + timedelta(minutes=slot)
        slots.append(dt)
    return slots


# Some Dummy values 
start_time = '9:00'
end_time = '15:00'
slot_time = 30
days = 2
start_date = datetime.now().date()

for i in range(days):
    date_required = datetime.now().date() + timedelta(days=1)
    print get_daily_slots(start=start_time, end=end_time, slot=slot_time, date=date_required)

输出如下:

[datetime.datetime(2017, 3, 31, 9, 0), datetime.datetime(2017, 3, 31, 9, 30), datetime.datetime(2017, 3, 31, 10, 0), datetime.datetime(2017, 3, 31, 10, 30), datetime.datetime(2017, 3, 31, 11, 0), datetime.datetime(2017, 3, 31, 11, 30), datetime.datetime(2017, 3, 31, 12, 0), datetime.datetime(2017, 3, 31, 12, 30), datetime.datetime(2017, 3, 31, 13, 0), datetime.datetime(2017, 3, 31, 13, 30), datetime.datetime(2017, 3, 31, 14, 0), datetime.datetime(2017, 3, 31, 14, 30), datetime.datetime(2017, 3, 31, 15, 0)]
[datetime.datetime(2017, 3, 31, 9, 0), datetime.datetime(2017, 3, 31, 9, 30), datetime.datetime(2017, 3, 31, 10, 0), datetime.datetime(2017, 3, 31, 10, 30), datetime.datetime(2017, 3, 31, 11, 0), datetime.datetime(2017, 3, 31, 11, 30), datetime.datetime(2017, 3, 31, 12, 0), datetime.datetime(2017, 3, 31, 12, 30), datetime.datetime(2017, 3, 31, 13, 0), datetime.datetime(2017, 3, 31, 13, 30), datetime.datetime(2017, 3, 31, 14, 0), datetime.datetime(2017, 3, 31, 14, 30), datetime.datetime(2017, 3, 31, 15, 0)]

或者你可以随意输出。