timestamper.online
Back to Guides

How to Handle Unix Timestamps in Python

Convert Unix timestamps in Python using the `datetime` and `time` modules.

1. Get Current Timestamp
python
import time

# Get current timestamp (seconds)
timestamp = int(time.time())
print(timestamp)
2. Convert Date to Timestamp
python
from datetime import datetime

# Date to timestamp
dt = datetime(2024, 1, 1)
timestamp = int(dt.timestamp())
3. Convert Timestamp to Readable Date
python
from datetime import datetime

# Timestamp to date
ts = 1704067200
dt = datetime.fromtimestamp(ts)
print(dt.strftime('%Y-%m-%d %H:%M:%S'))

Need to convert a specific timestamp right now?

Use Online Converter