Python timestamp to datetime Conversion Program

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

To work with dates and times, Python has a module called DateTime. It did not require any additional installation. It comes pre-installed with the Python package. A UNIX timestamp is the number of seconds between a specific date and January 1, 1970, at UTC. Unix timestamps are supported in the following formats: 10-digit epoch time format surrounded by brackets (or followed by a comma). The digits must be at the very start of the message. For example, [1234567890] or [1234567890, other] followed by the rest of the message.

What is Timestamp in Python?

A timestamp is a piece of encoded information that identifies the date and time of an occurrence in UNIX. This information could be milliseconds accurate. It is the datetime instance's POSIX timestamp.

A timestamp is a series of characters or encoded information that determines when a specific event occurred, typically providing the date and time of day to the second.

It comes pre-installed with the Python package. A UNIX timestamp is the number of seconds between a specific date and January 1, 1970, at UTC. You can see the below image showing the timestamp representation.

What is Datetime in Python?

Datetime is a single module in the Python programming language. This means that there are no distinct data types. You can import this datetime module to make it work with both dates and times. datetime is a Python module that comes with the language. Developers don't need to install it separately. It's right next to you. The datetime module allows you to work with various classes that work perfectly with date and time. You can find a variety of functions that deal with dates, times, and different time intervals within these classes.

Remember that date and datetime are two distinct objects when working with Python. If you change them, you change the objects, not the timestamps or strings.

DATE: It is used for values with a date part but no time part. MySQL retrieves and displays DATE values in YYYY-MM-DD format. The supported range is 1000-01-01 to 9999-12-31.

DATETIME: It is used for values containing date and time parts. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM format. The supported range is 1000-01-01 00:00:00 to 9999-12-31 23:59:59.

Implementation for Converting Timestamp to Datetime in Python

To obtain a date from a UNIX timestamp, use the DateTime module's fromtimestamp function. This function takes a timestamp as input and returns the date and time both.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax:

Parameters:

The fromtimestamp function takes two parameters, the first is timestamp and the second is timezone.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Return Type:

This function returns the date corresponding to a specified timestamp.

Code:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Output:

Explanation:

We've imported the DateTime class from the DateTime module here. Then we used the class method datetime.fromtimestamp(), which returns the local DateTime. The strftime function can be used to obtain a datetime in a specific format. The strftime() function converts Python timestamp to datetime. It takes one or more formatted code inputs and returns the string representation.

More Examples for Understanding

Example 1:

Code:

Output:

Explanation: When run, the above program will produce the output.

Example 2:

Code:

Output:

Explanation: We've imported the datetime class from the datetime module here. Then we applied datetime. The fromtimestamp() classmethod returns the current date and time (datetime object). This object is stored in the dt_object variable.

Note: Use the strftime() method to easily create a string representing date and time from a datetime object.

Similar Python Programs

Read more about datetime and Python timestamp to datetime conversion:

Conclusion:

Let's conclude our topic Python timestamp to datetime by mentioning some of the important points:

  • To work with dates and times, Python has a module called DateTime. It did not require any additional installation.
  • A timestamp is a piece of encoded information that identifies the date and time of an occurrence in UNIX. This information could be milliseconds accurate. It is the datetime instance’s POSIX timestamp.
  • Datetime is a single module in the Python programming language. This means that there are no distinct data types.
  • When working with Python, keep in mind that date and datetime are two distinct objects. If you change them, you’re changing the objects, not the timestamps or strings.
  • You can find a variety of functions that deal with dates, times, and different time intervals within these classes.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more