Calculating the time difference between two datetime values ​​in Python

I want to compare two dates and times with each other, and then use this information for other things. For example, if delta > 23hours to do it, elif delta > 11hours, to do it, etc. I thought it would be a reasonable way to write it, but python will not accept it! It says:

ValueError: 'h' is a bad directive in format '%m/%d/%Y%h:%m:%s'

Not a hstandard way to write a clock in Python ?: Oh

My dates are written in this format: "12/28/13 16:49:19", "m / d / yh: m: s", if that helps!

from datetime import datetime
date_format = "%m/%d/%Y%h:%m:%s"
then=(dictionary["date"])
now= time.strftime("%c")
a = datetime.strptime(str(then), date_format)
b = datetime.strptime(str(now), date_format)
delta = b - a
print(delta.hour)
+4
source share
1 answer

24- %H, H, . . , , y:

date_format = "%m/%d/%y %H:%M:%S"

. strftime() strptime(). %H .

, time.strftime('%c') , , :

b = datetime.now()

datetime.timedelta hours; :

delta = b - a
print(delta.total_seconds() // 60)

delta timedelta():

if delta > timedelta(hours=23):

:

>>> from datetime import datetime
>>> date_format = "%m/%d/%y %H:%M:%S"
>>> datetime.strptime('12/28/13 16:49:19', date_format)
datetime.datetime(2013, 12, 28, 16, 49, 19)
+7

All Articles