[Django]-Python django subtract time field

6πŸ‘

βœ…

>>> import datetime
>>> time1 = datetime.datetime.strptime("6:30",'%H:%M')
>>> time2 = datetime.datetime.strptime("7:30",'%H:%M')
>>> difference = time2-time1
>>> difference
datetime.timedelta(0, 3600)
>>> difference.seconds
3600
>>> "{:0>8}".format(datetime.timedelta(seconds=difference.seconds))
'01:00:00'
>>> "{:0>8}".format(datetime.timedelta(seconds=666777))
'7 days, 17:12:57'
πŸ‘€Rizwan Mumtaz

0πŸ‘

>>> from datetime import datetime
>>> s1 = "06:30"
>>> s2 = "07:30"
>>> timeformat = "%I:%M"
>>> delta = datetime.strptime(s2, timeformat) - datetime.strptime(s1, timeformat)
>>> delta
datetime.timedelta(0, 3600)
>>> delta.seconds
3600

delta is the time difference. You can check the value and convert accordingly.

πŸ‘€Barun Sharma

Leave a comment