Skip to content Skip to sidebar Skip to footer

Python - How To Display Time As Epoch?

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

Solution 1:

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.

Solution 2:

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.

Post a Comment for "Python - How To Display Time As Epoch?"