在python中查找当前时间间隔?
如何在 python 中找到最近的 15(或 10)分钟间隔? 例如,
>>> datetime.datetime.now()
datetime.datetime(2011, 2, 22, 15, 43, 18, 424873)
我想要当前 15 分钟间隔(15:30-15:44),所以我想将上面的日期时间转换为
datetime.datetime(2011, 2, 22, 15, 30, 00, 00)
How can I find the nearest 15 (or 10) minute interval in python ?
e.g.
>>> datetime.datetime.now()
datetime.datetime(2011, 2, 22, 15, 43, 18, 424873)
I'd like the current 15 minute interval (15:30-15:44) so I'd like to transform the above datetime to a
datetime.datetime(2011, 2, 22, 15, 30, 00, 00)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说最简单的方法:
希望有帮助。
Quite the easiest way to me:
Hope that'll help.
我认为对于这种情况,使用 replace 比 timedelta:
I think for this case using replace is more straightforward than timedelta:
看看 timedelta()
http://docs.python.org/发布/2.5.2/lib/datetime-timedelta.html
Look at timedelta()
http://docs.python.org/release/2.5.2/lib/datetime-timedelta.html
只需将分钟设置为
0,并将较短的时间单位(秒、毫秒、..)设置为 0
Just set minutes on
and set shorter time units (seconds, msec, ..) on 0