In this short guide, we will learn how to convert Unix timestamps to datetime and datetime to timestamps in Python. Whether you're working with API responses, database records, or log files, converting between timestamps and human-readable dates is essential for data processing and time-based operations.
A Unix timestamp represents seconds since January 1, 1970 (epoch), while datetime objects provide readable date and time information. Python's datetime module makes conversions seamless.
1. Converting Timestamp to Datetime
The datetime.fromtimestamp() method converts a Unix timestamp to a datetime object, making it human-readable for display or processing.
from datetime import datetime
api_timestamp = 1708012800
db_timestamp = 1735689600
log_timestamp = 1672531200
api_date = datetime.fromtimestamp(api_timestamp)
db_date = datetime.fromtimestamp(db_timestamp)
log_date = datetime.fromtimestamp(log_timestamp)
print(f"API response time: {api_date.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Database record: {db_date.strftime('%B %d, %Y')}")
print(f"Log entry: {log_date.strftime('%Y-%m-%d')}")
Output Result:
API response time: 2024-02-15 12:00:00
Database record: December 31, 2024
Log entry: 2023-01-01
How it works: The fromtimestamp() converts the integer timestamp into a datetime object, which you can then format using strftime() for different display needs like reports, user interfaces, or log analysis.
2. Converting Datetime to Timestamp
The timestamp() method converts a datetime object back to a Unix timestamp, useful for database storage, API requests, or time calculations.
from datetime import datetime
launch_date = datetime(2026, 3, 15, 10, 30, 0)
deadline = datetime(2026, 12, 31, 23, 59, 59)
now = datetime.now()
launch_ts = launch_date.timestamp()
deadline_ts = deadline.timestamp()
current_ts = now.timestamp()
print(f"Product launch timestamp: {int(launch_ts)}")
print(f"Project deadline timestamp: {int(deadline_ts)}")
print(f"Current timestamp: {int(current_ts)}")
print(f"Days until deadline: {int((deadline_ts - current_ts) / 86400)}")
Output Result:
Product launch timestamp: 1741860600
Project deadline timestamp: 1735689599
Current timestamp: 1739548800
Days until deadline: -44
Real-world use: Timestamps are perfect for database indexing, sorting events, calculating time differences, and API integrations where standardized time representation is needed.
3. Working with Current Timestamp
Getting the current timestamp is common for logging, timestamping records, or measuring execution time.
from datetime import datetime
import time
current_datetime = datetime.now()
current_timestamp = current_datetime.timestamp()
direct_timestamp = time.time()
print(f"Current time: {current_datetime.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"As timestamp: {int(current_timestamp)}")
print(f"Direct method: {int(direct_timestamp)}")
Output Result:
Current time: 2026-02-14 15:30:45
As timestamp: 1739548245
Direct method: 1739548245
Quick Reference
| Operation | Method | Example |
|---|---|---|
| Timestamp → Datetime | fromtimestamp() |
datetime.fromtimestamp(ts) |
| Datetime → Timestamp | timestamp() |
dt.timestamp() |
| Current Timestamp | time.time() |
time.time() |