0
0
PythonHow-ToBeginner · 2 min read

Python How to Convert Timestamp to Datetime Easily

Use datetime.fromtimestamp(timestamp) from the datetime module to convert a timestamp to a datetime object in Python.
📋

Examples

Input0
Output1970-01-01 00:00:00
Input1686000000
Output2023-06-06 00:00:00
Input-1000000000
Output1938-04-24 22:13:20
🧠

How to Think About It

To convert a timestamp (which is seconds since 1970-01-01) to a readable date and time, you use Python's datetime module. The function fromtimestamp takes the timestamp number and returns a datetime object representing that exact moment.
📐

Algorithm

1
Get the timestamp value as input.
2
Import the datetime module.
3
Use datetime.fromtimestamp() with the timestamp.
4
Return or print the resulting datetime object.
💻

Code

python
from datetime import datetime

timestamp = 1686000000
converted_date = datetime.fromtimestamp(timestamp)
print(converted_date)
Output
2023-06-06 00:00:00
🔍

Dry Run

Let's trace converting timestamp 1686000000 to datetime.

1

Input timestamp

timestamp = 1686000000

2

Convert using fromtimestamp

converted_date = datetime.fromtimestamp(1686000000) -> 2023-06-06 00:00:00

3

Print result

print(converted_date) outputs '2023-06-06 00:00:00'

StepTimestampDatetime Result
11686000000N/A
216860000002023-06-06 00:00:00
3N/A2023-06-06 00:00:00
💡

Why This Works

Step 1: Timestamp meaning

A timestamp is the number of seconds since January 1, 1970 (called the Unix epoch).

Step 2: Using fromtimestamp

datetime.fromtimestamp() converts this number into a human-readable date and time.

Step 3: Result is datetime object

The result is a datetime object that you can format or use in your program.

🔄

Alternative Approaches

Using pandas to convert timestamp
python
import pandas as pd

timestamp = 1686000000
converted_date = pd.to_datetime(timestamp, unit='s')
print(converted_date)
Pandas is useful if you already use it for data analysis; it returns a Timestamp object similar to datetime.
Using time module to convert timestamp
python
import time

timestamp = 1686000000
converted_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
print(converted_date)
This returns a formatted string instead of a datetime object, useful for quick display.

Complexity: O(1) time, O(1) space

Time Complexity

Conversion uses a fixed number of operations regardless of input size, so it is O(1).

Space Complexity

Only a single datetime object is created, so space usage is constant O(1).

Which Approach is Fastest?

Using datetime.fromtimestamp() is fastest and simplest for single conversions; pandas adds overhead but helps with bulk data.

ApproachTimeSpaceBest For
datetime.fromtimestamp()O(1)O(1)Simple, fast conversion
pandas.to_datetime()O(1) but heavierO(1)Data analysis with many timestamps
time.strftime() with localtimeO(1)O(1)Quick string formatting for display
💡
Always ensure your timestamp is in seconds, not milliseconds, before converting.
⚠️
Trying to convert a timestamp in milliseconds directly without dividing by 1000 causes wrong dates.