Max P Max P - 2 months ago 22
Python Question

Substract epoch time from datatime.datatime.now

I was asked to get the value of a key from a JSON on a particular website and subtract that value with

.now
(it's an expiration date).

I'm doing it using a
requests.get
as well as a
json.load
. My problem is: The value that I'm getting is epoch time and
datetime.datetime.now
is not.

What's the easiest way to (
datetime.datetime.now - my key(epoch value)
)? See values in screenshot below. I've tried converting from epoch to "human date" then do a subtraction with it but nope.

see both values here datetime.datetime.now on top and epoch from json on the bottom

EDIT: This may not be a duplicate because it asks about processing a timestamp and wants number of seconds between that timestamp and now.

Answer Source

You did say easiest. When a timestamp is involved think of the arrow library.

>>> time_string = '2017-08-14 11:52:09.145000'
>>> import arrow
>>> time_diff = arrow.now()-arrow.get(time_string)
>>> time_diff.total_seconds()
17604.346025