在python中减去两次 [英] subtract two times in python

查看:142
本文介绍了在python中减去两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 datetime.time 值,退出输入我想做一些像:

  duration = exit  -  enter 

但是,我收到此错误:

  TypeError:对于 - :'datetime.time'和'datetime.time 

不支持的操作数类型如何正确执行?一个可能的解决方案是将时间变量转换为 datetime 变量,然后重新输入,但我确定你们必须拥有

解决方案

尝试这样:



<$ p $日期

datetime.combine(date.today(),exit) - datetime.combine(date.today(),enter)

combine 构建一个可以减去的日期时间。 / p>

I have two datetime.time values, exit and enter and I want to do something like:

duration = exit - enter

However, I get this error:

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time

How do I do this correctly? One possible solution is converting the time variables to datetime variables and then subtruct, but I'm sure you guys must have a better and cleaner way.

解决方案

Try this:

from datetime import datetime, date

datetime.combine(date.today(), exit) - datetime.combine(date.today(), enter)

combine builds a datetime, that can be subtracted.

这篇关于在python中减去两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆