The fromtimestamp() function is used to return the date corresponding to a specified timestamp.
Note: Here the timestamp is ranging from the year 1970 to the year 2038, and this function does not consider leap seconds if any present in the timestamp. This function is a class method.
Syntax: @classmethod fromtimestamp(timestamp)
Parameters: This function accepts a parameter which is illustrated below:
- timestamp: This is the specified timestamp for which the date is going to be returned.
Return values: This function returns the date corresponding to a specified timestamp.
Example 1: Getting a date corresponding to an Epoch & Unix Timestamp.Â
Python3
# Python3 code to demonstrate# Getting a date corresponding# to a specified timestamp  # Importing datetime and time module import datetimeimport time  # Calling the time() function# to return current timeTodays_time = time.time()  # Printing today's timeprint(Todays_time)    # Calling the fromtimestamp() function# to get date from the current timedate_From_CurrentTime = datetime.date.fromtimestamp(Todays_time);  # Printing the current dateprint("Date for the Timestamp is: %s"%date_From_CurrentTime); |
Output:
1627279008.95 Date for the Timestamp is: 2021-07-26
Example 2: Getting a date corresponding to a specified timestamp.
Python3
# Python3 code to demonstrate# Getting a date corresponding# to a specified timestamp  # Importing datetime and time module import datetimeimport time  # Initializing a timestamp valueTimestamp = 1323456464;  # Calling the fromtimestamp() function# over the above specified Timestampdate_From_Timestamp = datetime.date.fromtimestamp(Timestamp);  # Printing the dateprint("Date for the Timestamp is: %s"%date_From_Timestamp); |
Output:
Date for the Timestamp is: 2011-12-09
