Introduction

A Timestamp is a way to represent a specific point in time, often used to log events or record when data is collected. By leveraging Python's datetime module, we can parse, manipulate and output timestamps in a wide variety of formats. Through this tutorial, we will cover the basics of timestamps in Python, including how to create, convert, and manipulate them to make your programs more efficient and effective. So, let's get started with Timestamps in Python!

Table of Contents :

  • Timestamp in Python
  • Getting timestamp in python :
    • using time module
    • using calendar module
    • get timestamp in Milliseconds
    • Get the UTC Timestamp
    • with a different timezone

Timestamp in Python

  • A timestamp is a numerical representation of a specific moment in time.
  • In Python, timestamps are usually represented as the number of seconds or milliseconds since the Unix epoch, 
  • The date January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch.
  • For example, the timestamp for the date 20th March 2020 is :   1583539200 

Getting timestamp in Python

  • There are different methods to get timestamp value in python.
  • Below we have discussed some methods to get timestamp.

Get Timestamp using time module in python :

  • The time module of python can be used to get the current timestamp.
  • The  time()  function of the  time  module can be used to get the current timestamp.
  • The  time()  function returns current time as the number of seconds since the Unix Epoch.
  • The date January 1st, 1970 at 00:00:00 UTC is referred to as the Unix epoch.
  • Code Sample : 

import time

current_timestamp = time.time()
print("Current timestamp:", current_timestamp)

# Output
# Current timestamp: 1646153094.983439


Get Timestamp using calendar Module in python:

  • The  calendar  module's  timegm()  function can also be used to get a timestamp from a time tuple.
  • Code Sample : 

import calendar

time_tuple = (2022, 3, 7, 16, 0, 0)
timestamp = calendar.timegm(time_tuple)

print("Timestamp :", timestamp)

# Output: 
# Timestamp: 1646157600


Get Timestamp in Milliseconds:

  • We can get the timestamp in seconds using the  time()  function.
  • Multiplying a timestamp by 1000 will convert it to milliseconds.
  • Code Sample : 

import time

current_timestamp = time.time()
milliseconds_timestamp = int(current_timestamp * 1000)
print("Timestamp in milliseconds:", milliseconds_timestamp)

# Output 
# Timestamp in milliseconds: 1646156186541


Get the UTC Timestamp:

  • The  gmtime()  function of the  time  module can be used to get the current UTC timestamp.
  • Code Sample : 

import time

current_utc_timestamp = time.mktime(time.gmtime())
print("Current UTC timestamp:", current_utc_timestamp)

# Output
# Current UTC timestamp: 1646149462.0


Get Timestamp from Datetime with a Different Timezone:

  • The  astimezone()  method can be used to get a  datetime  object with a specific timezone, 
  • This  datetime  object can then be converted to a timestamp using the  timestamp()  method.
  • Code Sample : 

from datetime import datetime
import pytz

tz = pytz.timezone('US/Eastern')
now = datetime.now(tz)
timestamp = now.timestamp()

print("Timestamp with US/Eastern timezone:", timestamp)

# Output
# Timestamp with US/Eastern timezone: 1646161716.082119



 

Prev. Tutorial : Get current time

Next Tutorial : timestamp to datetime