在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 44705I 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.0Calling .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更多推荐
发布评论