计算python中两次之间的差异 [英] Calculate the difference between two times in python

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

问题描述

我正在使用python,并且想计算两次之间的差异.

I am using python, and want to calculate the difference between two times.

实际上我有一个场景来计算登录和注销时间之间的差异,例如,在组织中,工作时间有一些特殊限制,所以如果用户在早上 9:00 AM 登录,如果他在晚上 6:00 PM 注销,我们需要计算他在办公室呆了多长时间(在当前场景中为 9 小时),但我想在 python 中执行此操作,所以任何人都可以让我知道如何实现上述计算登录和注销时间差异的概念?

Actually i had scenario to calculate the difference between login and logout times, for example in organizations there is some particular limit for working hours, so if a user login at 9:00 AM in the morning and if he logs out at 6:00 PM in the evening, we need to calculate how much duration he stayed in the office(which is 9 hours in the present scenario ), but i want to do this in python, so can anyone please let me know how to achieve the above concept of calculating the difference between login and logout times ?

推荐答案

>>> start = datetime.datetime(year=2012, month=2, day=25, hour=9)
>>> end = datetime.datetime(year=2012, month=2, day=25, hour=18)
>>> diff = end - start
>>> diff
datetime.timedelta(0, 32400)
>>> diff.total_seconds()
32400
>>> diff.total_seconds() / 60 / 60
9
>>> 

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

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