5

I need to display the current time as an epoch in python to pass as a variable into some code how do I go about doing that?

ie. 1458410416

Every method of

import datetime
datetime.datetime()

gives human readable values.

1

2 Answers 2

15

There is a built-in function in the time module:

import time

seconds = time.time()

There are many functions in the time module for seconds since the Epoch, time tuples, etc. I would recommend that you check out the docs.

10

Import the time library, and then use this code:

>>> time.mktime(datetime.datetime.now().timetuple())
1458410651.0

time.mktime converts a datetime to a Unix epoch timestamp.

2
  • 3
    great! I actually found this the same time I posted and did this epoch_time = int(time.time())
    – whoisearth
    Commented Mar 19, 2016 at 18:05
  • 1
    @whoisearth That's a good solution that I didn't know about, but I don't think that could be used if your desired time to convert isn't the current time. Commented Mar 19, 2016 at 18:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.