fromtimestamp()

fromtimestamp() : returns date and time by using POSIX timestamp

Get all details about Timestamp and generate timestamp
by using Calendar and time sliders.


Getting present timestamp
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

Example 1: Converting a Timestamp to a Readable Date

from datetime import datetime

timestamp = 1609459200  # January 1, 2021
dt = datetime.fromtimestamp(timestamp)

print(dt)  # Output: 2021-01-01 00:00:00

Example 2: Handling Time Zones

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)

Example 3: Converting the Current Time to a Timestamp

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

Example 4: Converting Future Timestamps

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

Example 5: Handling Timestamps Before Epoch (Negative Timestamps)

negative_timestamp = -123456789
dt = datetime.fromtimestamp(negative_timestamp)

print(dt)  # Output: Date before epoch
Output
OSError: [Errno 22] Invalid argument

You can use above mentioned timestamp genegrator and use this as input to generate date and time.

All Date Objects
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate 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.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer