这可能是一个非常简单的问题,但我花了很多时间来解决这个问题。我有这样的代码:
st_date=parser.parse(start_date)
en_date=parser.parse(end_date)
sel=[np.datetime64(st_date)+np.timedelta64(xx,'h') for xx in range(0,(diff.days*24+diff.seconds/3600))]
这会生成日期和时间列表,但是时间开始时间是当前时间,我想将时间开始为01:00,结束时间为23:00。
非常感谢任何建议
答案 0 :(得分:1)
您可以在生成序列之前将st_date
截断到几小时:
st_date = st_date.replace(minute=0, second=0, microsecond=0)
例如,如果您使用内置的datetime
模块:
import datetime
# initialize the start and end date
st_date = datetime.datetime.now()
en_date = st_date + datetime.timedelta(days = 2.2)
diff = en_date - st_date
# truncate the start date to hours
st_date = st_date.replace(minute=0, second=0, microsecond=0)
# generate sequence
sel=[st_date + datetime.timedelta(hours = xx) for xx in range(0, diff.days*24 + diff.seconds/3600)]
sel
#[datetime.datetime(2016, 12, 29, 21, 0),
# datetime.datetime(2016, 12, 29, 22, 0),
# datetime.datetime(2016, 12, 29, 23, 0),
# datetime.datetime(2016, 12, 30, 0, 0),
# datetime.datetime(2016, 12, 30, 1, 0),
# datetime.datetime(2016, 12, 30, 2, 0),
# datetime.datetime(2016, 12, 30, 3, 0),
# datetime.datetime(2016, 12, 30, 4, 0),
# datetime.datetime(2016, 12, 30, 5, 0),
# datetime.datetime(2016, 12, 30, 6, 0),
# datetime.datetime(2016, 12, 30, 7, 0),
# datetime.datetime(2016, 12, 30, 8, 0),
# ...]