How do I get the current Unix timestamp in Python? I really want to know how many seconds have elapsed since January 1st, 1970.
Asked
Active
Viewed 3,753 times
-1
-
Your question isabout getting the unix timestamp - you can do this using datetime.now().timestamp() - see https://docs.python.org/3/library/datetime.html?highlight=re#datetime.datetime.timestamp - which is partof the dupes above. Or you can subtract datetime(1970,1,1,0,0,0) from datetime.now() and get its total_seconds() - wich is also described in the dupes. Still sure this is not a dupe? – Patrick Artner Oct 14 '19 at 20:39
-
1@Patrick Artner The dupe I left up is actually real. It's just very disappointing that Googling for "seconds since epoch Python" gets you to a different answer of mine that's to an entirely different question. – nmichaels Oct 14 '19 at 20:47
1 Answers
3
>>> import time
>>> time.time()
1571084862.598271
Convert it to an integer, if required, with
int(time.time())

nmichaels
- 49,466
- 12
- 107
- 135
-
It's worth noting here that [Python's `int` function simply trims off the decimal value, i.e. it rounds down](https://stackoverflow.com/a/43661374/1797103). This is important because rounding up would result in a fraction of a second that doesn't exist. – Ryan Allen Jun 16 '23 at 16:27