-1

I need to check if some date and now arent' different more than in 18 hours:

dt = parser.parse("some date 123") #working well

diff = datetime.datetime.now() - dt
print "datetime.datetime.now() - dt = %s" % diff  # 31 days, 0:08:04.882498 -- correct
print "datetime.datetime.now() - dt seconds = %s" % diff.seconds # 484 -- too

(datetime.datetime.now() - dt).seconds / 3600) < 18 # returns True always

As you can see, even though the dates are different in 31 days, the amount of seconds if very little which doesn't allow me to calculate the amount of hours. Why is it so small? Should it be x * 60 * 60 * amount_of_days? And how can I do what I want?

Incerteza
  • 32,326
  • 47
  • 154
  • 261

2 Answers2

1

You may try this:

from datetime import date

date0 = date(2014, 9, 07)
date1 = date(2014, 9, 05)
diff = date0 - date1
print diff.days * 24

Also check How do I check the difference, in seconds, between two dates?

or

>>> from datetime import datetime
>>> date0 = datetime(2014,09,07,0,0,0)
>>> date1 = datetime(2014,09,01,23,59,59)
>>> date0-date1
datetime.timedelta(6, 1)
>>> (date0-date1).days * 24
144
Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

seconds is returning just the number of seconds, not the total number of seconds, for that you can use timedelta.total_seconds.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115