如何在python中获得两个日期的差异? [英] How can I get a difference of two dates in python ?

查看:68
本文介绍了如何在python中获得两个日期的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有与python相关的查询。我希望得到2个不同时间的时间差。其中一次,我作为字符串获得(作为手动输入)

第二次是当地时间。

以下是源代码:

Hello I have query related to python. I want to get difference of time span of 2 different times. One of the time, which I obtain as string (As a manual input)
second time is local time.
Here is source code below :

from time import strptime
from time import localtime
from datetime import date
from datetime import datetime

out="20150827173103"
time1=datetime(time.strptime(out,"%Y%m%d%H%M%S"))
print(time1)           #string converted into time format#

time2=time.strftime("%Y%m%d%H%M%S")
print(time2)           #got local time in string format#

time3=datetime(time.strptime(time2,"%Y%m%d%H%M%S")    #local time in string is converted into time format#
diff=time3-time1
print(diff)



错误:ValueError:('年必须在1..9999',(2015年,8,27,17,31,3,3,239,-1))在第6行。

请建议我如何才能得到这个问题的解决方案。


Error: ValueError: ('year must be in 1..9999', (2015, 8, 27, 17, 31, 3, 3, 239, -1)) at 6th line.
Please suggest how can I get the solution of this problem.

推荐答案

previous=date(2015,8,27)
current=date.today()
difference=current - previous
print(difference)



类似的代码代码


Similar code for times

t1=datetime.strptime("20150827130100", "%Y%m%d%H%M%S")
t2=datetime.today()
tdiff=t2-t1
tdiff


这篇关于如何在python中获得两个日期的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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