0

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.

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
Max P
  • 3
  • 1
  • This may **not** be a **duplicate** because it asks about processing a **timestamp** and wants number of seconds **between** that timestamp and now . – Bill Bell Aug 18 '17 at 19:35

2 Answers2

1

Use the python dateutil module to convert your epoch value to a date and then just subtract them like:

diff = datetime.now() - my_date

You could also use the relativedelta class if you want the output as a number of days, months, years, etc.

Momus
  • 394
  • 2
  • 13
0

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
Bill Bell
  • 21,021
  • 5
  • 43
  • 58