Open In App

Python program to convert unix timestamp string to readable date

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see the conversion of the Unix timestamp string to a readable date. This can be done with the help of fromtimestamp() and strftime() functions in Python.

Example: 

Input:  1294113662
Output: 2011-01-04 09:31:02
Explanation: Unix timestamp string to a readable date 

What is fromtimestamp()?

The fromtimestamp() function is used to return the date and time corresponding to a specified timestamp.

Note: Here the timestamp is ranging from the year 1970 to the year 2038, and this function does not consider leap seconds if any present in the timestamp. This function is a class method.

Syntax of fromtimestamp()

Syntax:  @classmethod fromtimestamp(timestamp)

Parameters: This function accepts a parameter which is illustrated below:

  • timestamp: This is the specified timestamp for which the date is going to be returned.

Return values: This function returns the date and time corresponding to a specified timestamp.

What is strftime()?

The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string representation.

Syntax of strftime()

Syntax: strftime(format)

Parameter: This function accepts a single parameter which is illustrated below:

  • format: This is the specified date and time object.

Return Values: It returns the string representation of the date or time object.

Convert Unix timestamp string to readable date in Python

Example 1: Convert the specified Unix timestamp string.

Python3




# Importing datetime module
import datetime
 
# Calling the fromtimestamp() function to
# extract datetime from the given timestamp
 
# Calling the strftime() function to convert
# the extracted datetime into its string format
print(datetime.datetime.fromtimestamp(int("1294113662"))
      .strftime('%Y-%m-%d %H:%M:%S'))


Output:

2011-01-04 09:31:02

Example 2: Convert the specified Unix timestamp string.

Python3




# Importing datetime module
import datetime
 
# Calling the fromtimestamp() function to
# extract datetime from the given timestamp
timestamp = datetime.datetime.fromtimestamp(1200001234)
 
# Calling the strftime() function to convert
# the extracted datetime into its string format
print(timestamp.strftime('%Y-%m-%d %H:%M:%S'))


Output:

2008-01-11 03:10:34


Previous Article
Next Article

Similar Reads

How to Convert DateTime to UNIX Timestamp in Python ?
The Unix timestamp is a single signed integer that grows by one every second, allowing computers to store and manipulate conventional date systems. The software is then translated into a human-readable format. The Unix timestamp is the number of seconds calculated since January 1, 1970. In this article, we are going to see how to convert DateTime t
5 min read
Convert datetime to unix timestamp in SQLAlchemy model
When dealing with databases date and time are considered to be one of the most important attributes for any entity. With such data, we often encounter some common task of converting DateTime to a Unix timestamp. In this article, we will learn how we can convert datetime to Unix timestamp in SQLAlchemy. Converting Datetime to Unix TimeStamp in SQLal
5 min read
Python | Pandas Timestamp.timestamp
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.timestamp() function returns the time expressed as the number of seconds that have passed since January 1, 1970. That z
3 min read
Convert date string to timestamp in Python
The Most Common way we use to store dates and times into a Database is in the form of a timestamp. date and time in form of a string before storing it into a database, we convert that date and time string into a timestamp. Python provides various ways of converting the date to timestamp. Few of them discussed in this article are: Using timetuple()U
2 min read
How to convert timestamp string to datetime object in Python?
Python has a module named Datetime to work with dates and times. We did not need to install it separately. It is pre-installed with the Python package itself. A UNIX timestamp is several seconds between a particular date and January 1, 1970, at UTC. Input: "2023-07-21 10:30:45"Output: 2023-07-21 10:30:45Explanation: In This, We are converting a tim
2 min read
Python | Pandas Timestamp.date
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Timestamp.date() function return a datetime object with same year, month and day as that of the given Timestamp object. Syntax :
2 min read
Python Pandas - Convert PeriodIndex object to Timestamp and set the frequency
In this article, we will discuss how to convert period index objects to timestamp and set the frequency in the python programming language. The pandas PeriodIndex.to_timestamp() method is used to convert a PeriodIndex object to Timestamp and set the frequency. frequency can be set using the 'freq' parameter of the method. Example 1: Pandas package
3 min read
Convert Datetime to UTC Timestamp in Python
Dealing with datetime objects and timestamps is a common task in programming, especially when working with time-sensitive data. When working with different time zones, it's often necessary to convert a datetime object to a UTC timestamp. In Python, there are multiple ways to achieve this. In this article, we will explore four different methods for
3 min read
Check If File is Readable in Python
We are given a file and we have to check whether the file is readable in Python or not. In this article, we will see how we can check if a file is readable or not by using different approaches in Python. How to Check if a File is Readable in PythonBelow, are the methods of How to Check If a File Is Readable in Python: Using os ModuleUsing Try Excep
3 min read
How to convert NumPy datetime64 to Timestamp?
In this article, we will discuss how to convert NumPy datetime64 to Timestamp in Python.Example: Input: If the current datetime64 is as follows: 2020-08-18 09:31:51.944622 Output: The required timestamp in seconds will be: 1597743111.944622 The required timestamp in minutes will be: 26629051.8657437 The required timestamp in an hour will be: 443817
2 min read
Practice Tags :