from datetime import datetime
timestamp = datetime.timestamp(datetime.now())
print("Present Timestamp : ", timestamp)
Output
Present Timestamp : 1568717876.556073
Using Timestamp as input we will use fromtimestamp() to get date and time
from datetime import datetime
timestamp = datetime.timestamp(datetime.now())
print("Present Timestamp : ", timestamp)
dt = datetime.fromtimestamp(timestamp)
print(dt)
Output
Present Timestamp : 1568717943.760438
2019-09-17 16:29:03.760438
from datetime import datetime
timestamp = 1609459200 # January 1, 2021
dt = datetime.fromtimestamp(timestamp)
print(dt) # Output: 2021-01-01 00:00:00
from datetime import datetime, timezone
timestamp = 1609459200
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
print(dt) # Output: 2021-01-01 00:00:00+00:00 (UTC)
from datetime import datetime
current_time = datetime.now().timestamp() # Get current time as timestamp
dt = datetime.fromtimestamp(current_time)
print(dt) # Output: Current date and time
from datetime import datetime
future_timestamp = 1798787865 # 2027/01/01 07:17:45
dt = datetime.fromtimestamp(future_timestamp)
print(dt) # Output: 2027-01-01 12:47:45
negative_timestamp = -123456789
dt = datetime.fromtimestamp(negative_timestamp)
print(dt) # Output: Date before epoch
Output
OSError: [Errno 22] Invalid argument
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.