Get current timestamp

linkaiyi
Follow

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)
# 1591172581.0117

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)
# 1591172581.0117

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)
# 1591172581
Object has 0 attachments

Was this article helpful?

This article is viewed 11 times!

Recent Viewed Articles

0 Comments

Leave a Comment

Support