迭代datetime对象以生成字符串

时间:2015-12-12 01:04:32

标签: python datetime

我有2个HashSet个对象,我想创建一个字符串列表,它按小时重新表示两个日期时间对象之间的递增。例如,

datetime

我想制作这种类型的列表

dt_obj1 = datetime.datetime(2015, 12, 11, 21, 42, 39, 381926)
dt_obj2 = datetime.datetime(2015, 12, 13, 13, 42, 39, 381926)

我不确定当一小时的增量改变日,月或年时如何更新字符串

3 个答案:

答案 0 :(得分:3)

我会创建一个类似于(t2-t1) = 2871 (t4-t3) = 26 的生成器,步长一小时:

range()

答案 1 :(得分:1)

我会使用真棒range_hourly()中的Delorean library函数:

$("p .glyphicon-remove").click(function(e){
    e.stopPropagation();
    $(this).hide();
});

打印:

import datetime

from delorean import range_hourly

dt_obj1 = datetime.datetime(2015, 12, 11, 21, 42, 39, 381926)
dt_obj2 = datetime.datetime(2015, 12, 13, 13, 42, 39, 381926)

for dt in range_hourly(start=dt_obj1, stop=dt_obj2):
    print(dt.datetime.strftime("%Y%m%d-%H%M"))

答案 2 :(得分:0)

您可以使用pandas.DatetimeIndex

>>> import pandas as pd
>>> print pd.DatetimeIndex(start=datetime.datetime(2015, 12, 11, 21, 42, 39, 3819
26), end=datetime.datetime(2015, 12, 13, 13, 42, 39, 381926), freq=pd.DateOffset
(hours=1)).to_pydatetime()
array([datetime.datetime(2015, 12, 11, 21, 42, 39, 381926),
   datetime.datetime(2015, 12, 11, 22, 42, 39, 381926),
   datetime.datetime(2015, 12, 11, 23, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 0, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 1, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 2, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 3, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 4, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 5, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 6, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 7, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 8, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 9, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 10, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 11, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 12, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 13, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 14, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 15, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 16, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 17, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 18, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 19, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 20, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 21, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 22, 42, 39, 381926),
   datetime.datetime(2015, 12, 12, 23, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 0, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 1, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 2, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 3, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 4, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 5, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 6, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 7, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 8, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 9, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 10, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 11, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 12, 42, 39, 381926),
   datetime.datetime(2015, 12, 13, 13, 42, 39, 381926)], dtype=object)