gpt4 book ai didi

python - 使用 Python 日期时间计算时间窗口

转载 作者:行者123 更新时间:2023-12-01 06:13:38 35 4
gpt4 key购买 nike

我想在给定 datetime.datetime 时间戳(例如 datetime.datetime(2010, 12, 21) 的情况下,查询我的数据库以查找在特定日期(例如今天 00:00 到 23:59:59 之间)发布的所有记录、17、59、43、85335)。

计算开始和结束 datetime.datetime 实例的最佳方法是什么?我想最终得到这样的结果:

>>> timestamp = datetime.datetime(2010, 12, 21, 17, 59, 43, 85335)
>>> # do something with timestamp to get start_date and end_date
>>> start_date
datetime.datetime(2010, 12, 21, 0, 0, 0, 0)
>>> end_date
datetime.datetime(2010, 12, 21, 23, 59, 59, 0)

谢谢,HC

最佳答案

啊,找到我想要的了。我每次都在日期时间上挣扎。

>>> timestamp = datetime.datetime.now()
>>> timestamp
datetime.datetime(2010, 12, 21, 18, 31, 37, 900795)
>>> timestamp.replace(hour=0, minute=0, second=0, microsecond=0)
datetime.datetime(2010, 12, 21, 0, 0)

关于python - 使用 Python 日期时间计算时间窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4501372/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com