Skip to content
Related Articles

Related Articles

Improve Article

fromtimestamp() Function Of Datetime.date Class In Python

  • Difficulty Level : Basic
  • Last Updated : 28 Jul, 2021

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 datetime
import time
  
# Calling the time() function
# to return current time
Todays_time = time.time()
  
# Printing today's time
print(Todays_time)
  
  
# Calling the fromtimestamp() function
# to get date from the current time
date_From_CurrentTime = datetime.date.fromtimestamp(Todays_time);
  
# Printing the current date
print("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 datetime
import time
  
# Initializing a timestamp value
Timestamp = 1323456464;
  
# Calling the fromtimestamp() function
# over the above specified Timestamp
date_From_Timestamp = datetime.date.fromtimestamp(Timestamp);
  
# Printing the date
print("Date for the Timestamp is: %s"%date_From_Timestamp);

Output:

Date for the Timestamp is: 2011-12-09

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :