How to get Unix Timestamp in Python

To print timestamp in Python is very simple, all you have to do is, import the time library and call the time() function to get the timestamp.

But the problem most of us facing is where it will return a float result like this:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import time
>>> print (time.time())
1428684549.944626

To get the actual Unix timestamp, just type cast the float value into int()

the result is,

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:15:05) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import time
>>> print (int(time.time()))
1428684706
>>>

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top