python - 如何生成包含时间的日期?
问题描述
我正在尝试生成具有范围的日期,但未显示时间。代码如下:
def date_range(start, end, step: datetime.timedelta):
while start < end:
yield start
start += step
for d in date_range(
start=datetime.date(2019, 1, 1),
end=datetime.date(2019, 1, 15),
step=datetime.timedelta(days=7),
):
print(d)
>>>2019-01-01
>>>2019-01-08
该代码仅显示日期而不显示时间。为什么???
解决方案
datetime.date()
根据此处的 Python 文档,您正在使用它不包含时间信息 - 它只是将小时/分钟/秒设置为 0。
我想你想使用datetime.datetime()
包含时间信息的。
有关更多信息, 请参阅Python 文档。datetime.datetime()
推荐阅读
- python - 如何使用 Python 在几秒钟内转换数据类型 (dd:hh:mm:ss) “1 day, 01:00:00”
- php - 如何准备数组中的数据以存储索引 0 和索引 1 中的条目,然后存储索引 1 到索引 2 的条目?
- java - Dagger2 找不到带注释的方法
- google-maps - 删除了谷歌地图 api 密钥的 http 引用列表
- excel - 如何在启动时检索文件名
- android - avc : 拒绝 { find } for service=android.os.UpdateEngineService
- freepascal - 为什么这里的 if 语句中需要序数表达式
- c# - 不让目标保持活动状态的委托
- reactjs - React/Redux 中的 Cloudinary “被 CORS 策略阻止”。请求头域授权
- r - 嵌套的 tibbles 函数没有给出预期的输出