[Fixed]-Subtract or add datetime fields which are returned from djangomodel

0👍

If you want to get the time difference then you should do it that way:

from datetime import timedelta    

date1 = subtask1.Created_subtask_date
date2 = subtask2.Created_subtask_date

diff = date1 - date2

print(type(diff))  # <class 'datetime.timedelta'>

print(diff.seconds)
print(diff.days)

More on timedelta.

👤nik_m

1👍

You do not need to convert them to Integers.. You can get difference of two dates easily. for e.g

from datetime import datetime
d = datetime(2017,3,17,12,50,59)
d2 = datetime.now()

e = d2 - d
print(e.days,e.seconds) # output 3 (days) 41569 (seconds)

Hope it helps.

Leave a comment