Introduction

Python provides us with many built-in modules to handle date and time easily. In this tutorial, we will focus on how to change timestamp to datetime in Python. Timestamp represents the time elapsed since a certain point in time, usually January 1st 1970, while datetime represents a specific date and time. Converting timestamp to datetime offers better readability of the data. We will use the datetime module to convert timestamp to datetime and vice versa. This tutorial is suitable for Python beginners who want to learn how to manipulate date and time data in their programs.

Table of Contents :

  • Convert timestamp to datetime 
    • Converting timestamp to datetime object
    • Converting timestamp to datetime string in specific format 
    • Converting timestamp directly to datetime string using str() function 
    • Converting timestamp to datetime object in UTC
  • Converting datetime to timestamp

Convert timestamp to datetime 

  • We can convert timestamp to datetime object using the  fromtimestamp()  method of the  datetime  class.
  • In this section we'll discuss three types of conversions :
    • Converting timestamp to datetime object
    • Converting timestamp to datetime string in specific format 
    • Converting timestamp directly to datetime string using str() function 
    • Converting timestamp to datetime object in UTC.

Converting timestamp to datetime object

  • The  fromtimestamp()  method of the  datetime  class can be used to convert a timestamp to a  datetime  object.
  • Code Sample : 

from datetime import datetime

timestamp = 1646153014.449067
dt_object = datetime.fromtimestamp(timestamp)

print("Datetime Object :", dt_object)
print("Data type of dt_object :", type(dt_object))

# Output
# Datetime Object : 2022-03-01 22:13:34.449067
# Data type of dt_object : <class 'datetime.datetime'>



Converting timestamp to datetime string in specific format 

  • We can convert the timestamp to  datetime  object using the  fromtimestamp()  method.
  • We can then use the  strftime()  method to format the  datetime  object as a string.
  • We can specify a desired format of the output string in the  strftime()  method.
  • Code Sample : 

from datetime import datetime

timestamp = 1646153014.449067
dt_object = datetime.fromtimestamp(timestamp)
datetime_string = dt_object.strftime("%d-%b-%Y %H:%M:%S")

print("Datetime string :", datetime_string)
print("Data type of dt_object datetime_string :", type(datetime_string))

# Output
# Datetime string : 01-Mar-2022 22:13:34
# Data type of dt_object datetime_string : <class 'str'>



Converting timestamp to datetime string using str() function 

  • We can convert the timestamp to  datetime  object using the  fromtimestamp()  method.
  • We can then use the  str()  method todirectly convert datetime object to string.
  • Code Sample : 

from datetime import datetime

timestamp = 1646153014.449067
dt_object = datetime.fromtimestamp(timestamp)
dt_str = str(dt_object)

print("Datetime String :", dt_str)
print("Data type of dt_str :", type(dt_str))


# Output
# Datetime String : 2022-03-01 22:13:34.449067
# Data type of dt_str : <class 'str'>



Converting timestamp to a datetime object in UTC

  • We can use the  utcfromtimestamp   function of  datetime  module to convert timestamp to datetime object in UTC.
  • Code Sample : 

from datetime import datetime

# timestamp
ts = 1337961600

dt_utc = datetime.utcfromtimestamp(ts)

print(dt_utc)

# Output
# 2012-05-25 16:00:00



Converting datetime to timestamp

  • The  timestamp()  method of the  datetime  class can be used to convert a  datetime  object to a timestamp.
  • Code Sample : 

from datetime import datetime

now = datetime.now()
timestamp = datetime.timestamp(now)

print("Timestamp :", timestamp)


# Output
# Timestamp: 1646153014.449067



Prev. Tutorial : Timestamp

Next Tutorial : Timedelta