Python - How to get current timestamps

By xngo on June 12, 2019

A timestamp is the number of seconds since Jan 1, 1970 00:00:00. There are multiple ways to get the current timestamp in Python. You can use functions from time, datetime, or calendar modules. Here are some examples.

# Using time.
import time
ts = time.time()
print("Using time    :", ts)
 
# Using datetime.
import datetime
ts = datetime.datetime.now().timestamp()
print("Using datetime: ", ts)
 
# Using calendar.
import calendar
import time
ts = calendar.timegm(time.gmtime())
print("Using calendar: ", ts)

Output

Using time    : 1560375010.7313197
Using datetime:  1560375010.732931
Using calendar:  1560375010

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.