Free cookie consent management tool by TermsFeed The fromtimestamp() method of datetime class in Python | Pythontic.com

The fromtimestamp() method of datetime class in Python

Overview:

  • Given a timestamp from the UNIX epoch i.e., the number of seconds passed since 1970, the class method fromtimestamp() creates a datetime object representing the corresponding local time.

  • If a timezone object or an object of a timezone subclass is provided through tzinfo parameter the timestamp is converted to the time and date of the specific timezone.

  • Leap seconds are ignored by the fromtimestamp() method. In timekeeping the leap seconds are the seconds added often one by one between many years to compensate for the slowing down of Earth’s rotation.

Example:

# Example Python program that creates a 
# datetime object from a given POSIX style
# timestamp string

import datetime as dt

# Create time from timestamp
fromEpochTillNow = 1753781262
td = dt.datetime.fromtimestamp(fromEpochTillNow)
print("Date and time from the timestamp:")
print(td)
print("Timezone:")
print(td.tzinfo)
print(type(td))

Output:

Date and time from the timestamp:
2025-07-29 14:57:42
Timezone:
None
<class 'datetime.datetime'>

 


Copyright 2025 © pythontic.com