Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 :
Python
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)

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.
Posted
Updated 3-Nov-15 2:53am
v2

1 solution

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

Similar code for times
Python
t1=datetime.strptime("20150827130100", "%Y%m%d%H%M%S")
t2=datetime.today()
tdiff=t2-t1
tdiff
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900