There is multiple ways how to get current timestamp in Python

Using module time

Module time is providing various time related functions. One of them is time which return number of seconds since the epoch.


import time;
ts = time.time()
print(ts)
# 1500931359.474279		

Using module datetime

Module datetime provides classes for manipulating date and time in more object oriented way. One of them is datetime.datetime.now which return number of seconds since the epoch.


import datetime;
ts = datetime.datetime.now().timestamp()
print(ts)
# 1500931359.474279

Using module calendar

Another way how to get current timestamp is to combine multiple functions from multiple modules. Python provides also calendar module. In this case we will use function calendar.timegm to convert tuple representing current time.


import calendar;
import time;
ts = calendar.timegm(time.gmtime())
print(ts)
# 1500931359

Tags

Related Articles

This website uses cookies to ensure you get the best experience on our website. More info