在Python中获得错误的时间增量(getting wrong time delta in python)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
在Python中获得错误的时间增量(getting wrong time delta in python)

我正在减去Python 2.7中的两个日期,并在几秒钟内得到错误的结果。 显然这些日期之间的差异超过24小时,即86400秒。 但我得到44705s,为什么是这样以及如何解决它?

>>> date1 datetime.datetime(2017, 10, 22, 11, 41, 28) >>> date2 datetime.datetime(2017, 10, 20, 23, 16, 23) >>> (date1-date2).seconds 44705

I am subtracting two dates in python 2.7 and getting wrong result in seconds. Apparently difference between these dates is more than 24h which is 86400s. But I am getting 44705s, why is that and how to fix it ?

>>> date1 datetime.datetime(2017, 10, 22, 11, 41, 28) >>> date2 datetime.datetime(2017, 10, 20, 23, 16, 23) >>> (date1-date2).seconds 44705

最满意答案

调用.seconds只会给你timedelta对象的秒部分, timedelta考虑秒,分钟和小时(详见文档)。 如果你想在几秒钟内完成timedelta ,请调用total_seconds 。

>>> (date1 - date2).total_seconds() 131105.0

Calling .seconds will only give you the seconds component of the timedelta object, which only takes into account seconds, minutes, and hours (see docs for more detail). If you want the entire timedelta in seconds, call total_seconds.

>>> (date1 - date2).total_seconds() 131105.0

更多推荐

本文发布于:2023-04-20 18:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9bde36ebf9e153fd2794976fd81e16fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:增量   错误   时间   Python   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!