Python Timestamp Examples with Time Module – POFTUT

Python Timestamp Examples with Time Module


Timestamp is used to hold the seconds from epoch. Epost is a generic term used to represent data January 1st, 1970 in UTC. So this means epoch and timestamp do not have any time zone or by default UTC time zone. In this tutorial we will look different examples about time  module with time stamp examples.

Import Time Module

As related functions are provided by modules we need to import time  module in order to use time() function. We will use classical python import to load.

import time

Get Timestamp with time() Function

We will use time()  function in order to create a timestamp for the current time. We will assign created timestamp into variable named ts and then print with print()  function like below.

import time      
ts = time.time() 
print(ts)
Get Timestamp with time() Function
Get Timestamp with time() Function

Get Timestamp with gmtime() Function

In this example we will use datetime  module and now() function in order to We will print is with print() function. We have to import datetime module before running now() function.

import datetime; 
ts = datetime.datetime.now().timestamp() 
print(ts)
Get Timestamp with gmtime() Function
Get Timestamp with gmtime() Function

Get Timestamp with Calender Module

We have alternatives while getting current time stamp. But most of them are related with time  module. We will use timegm()  function like below.

import calendar; 
import time; 
ts = calendar.timegm(time.gmtime()) 
print(ts)
Get Timestamp with Calender Module
Get Timestamp with Calender Module

LEARN MORE  What Is Linux Epoch Time?

Leave a Comment