python - 在 python 中列出两个日期之间的小时数

标签 python python-2.7 date time datetime-format

我有两次,我想在 Python 中使用相同的格式列出它们之间的所有时间

from= '2016-12-02T11:00:00.000Z'
to= '2017-06-06T07:00:00.000Z'
hours=to-from

所以结果会是这样的 2016-12-02T11:00:00.000Z 2016-12-02T12:00:00.000Z 2016-12-02T13:00:00.000Z ..... 等等 我该怎么做?我应该使用什么样的插件?

最佳答案

如果可能的话,我会推荐使用 pandas。

import pandas
time_range = pandas.date_range('2016-12-02T11:00:00.000Z', '2017-06-06T07:00:00.000Z', freq='H')

如果您需要字符串,请使用以下内容:

timestamps = [str(x) + 'Z' for x in time_range]
# Output
# ['2016-12-02 11:00:00+00:00Z',
#  '2016-12-02 12:00:00+00:00Z',
#  '2016-12-02 13:00:00+00:00Z',
#  '2016-12-02 14:00:00+00:00Z',
#  '2016-12-02 15:00:00+00:00Z',
#  '2016-12-02 16:00:00+00:00Z',
#  ...]

关于python - 在 python 中列出两个日期之间的小时数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44683774/

相关文章:

python - 为什么 scikit 学习分词器不能正确分词 utf-8(葡萄牙语)文本?

python - 从json模式动态创建mongoengine类

启动脚本时,Python 日志消息出现在 stdout 中

date - 如何读取 Neo4j 中以 MM/DD/YYYY 或 YYYY 表示日期的列

javascript - JavaScript 中 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?

Python,计算两个日期之间的天数在一种特定情况下返回错误值

python - 具有成本函数的 numpy 等高线图

python - 无法安装触发网络自动化工具

python - 更正在 TravisCI 上的 2.7.10 应用程序中打印 unicode 字符时的 unicode 错误

ios - 如何在我的 html5 日期输入上设置最大值,以便它可以被 iOS 版 safari 识别?