Python 不比较日期?

标签 python date datetime time compare

我有这个启动时间:

2015-01-15 10:31:54+00:00

我得到当前时间

current_time = datetime.datetime.now(launch_time.tzinfo)

我希望两次都相同,所以我使用了 tzinfo。所以,current_time的值为

2015-01-16 10:55:50.200571+00:00

我用这个来计算运行时间:

running_time = (current_time - launch_time).seconds/60

该值仅返回 23 分钟。应该是一天 + 23 分钟 = 1463 分钟

谁能帮帮我。谢谢

最佳答案

您忽略了返回的 timedelta 对象的 .days 属性。使用 timedelta.total_seconds()而不是将它们包含在一个值中:

running_time = (current_time - launch_time).total_seconds()/60

或者如果您想忽略增量的微秒部分,则显式使用它:

running_time = current_time - launch_time.total_seconds()
running_time = running_time.seconds / 60 + running_time.days / 1440

来自documentation for timedelta.total_seconds() :

Return the total number of seconds contained in the duration. Equivalent to (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 computed with true division enabled.

关于Python 不比较日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982695/

相关文章:

python - 无法使用具有实际名称的 flask 下载文件

python - PySpark DataFrames - 使用不同类型的列之间的比较进行过滤

javascript - 从日期数组中减去 1 个月

c# - 在开发环境中工作时,发布到 Controller 的 DateTime 在 azure 中变为 null

c# - 动态延迟 IObservable 值的延迟函数

python - "is"报告子进程通信错误()

python - 如果我读了一个文件之后没有关闭它会怎样?

mysql - 尝试创建一个 Access 2013 查询,该查询选择上一年 9 月 1 日到当年 8 月 31 日之间的日期

java - 如何获取当年的任何特定日期

oracle - 如何仅从 Oracle SQL Developer 的 DateTime 字段中提取时间?