-4

I would like to measure elapsed time in days with the Python time module. My attempt is:

import time, datetime

def elapsed_time(start)
    return datetime.datetime.fromtimestamp(time.time()-start).strftime('%Y-%m-%d-%H-%M-%S')

start = time.time()
print(elapsed_time(start))
# 1970-01-01-00-00-06

I would like the string to start from year 0, month 0, and day 0. How do I do that?

Toke Faurby
  • 5,788
  • 9
  • 41
  • 62

1 Answers1

1

Just use the difference between datetime.now() and your start timestamp. The resulting datetime.timedelta object has a days attribute:

from datetime import datetime

def elapsed_days(start):
    return (datetime.now() - start).days

start = datetime(2017, 11, 1)
>>> elapsed_days(start)
19

start = datetime(2017, 1, 1)
>>> elapsed_days(start)
323

>>> elapsed_days(datetime(1, 1, 1))
736652
mhawke
  • 84,695
  • 9
  • 117
  • 138