Updated April 3, 2023
Introduction to Timestamp to Date in Python
The timestamp to date is the conversion of the encoded numerical values into data using python. The timestamp is an encoded or encrypted sequence of the information and converts it into date and time. The python converts from timestamp to date using the installed module or method. It is a method to convert digital information into date using a python programming language. The python language has installed a feature to change timestamp to date using the method. It is a transformation in python technology to keep maintain application information.
Syntax of Timestamp to Date in Python
Given below is the syntax mentioned:
The timestamp to date converts from 1 January 1970, at UTC.
The basic syntax of the timestamp to date method is below:
fromtimestamp()
The “fromtimestamp” method helps to convert into date and date-time. The object of the value keeps inside of the method.
The basic syntax of the timestamp to date with value is below:
fromtimestamp(timestamp value)
Or
fromtimestamp(timestamp value, tz = none)
The “fromtimestamp” method helps to convert into a date. The “timestamp value” is a sequence of the information to convert into a date. The “tz” specifies the time zone of the timestamp. This function is optional.
The syntax of the timestamp to date using python language is below:
datetime.fromtimestamp(timestamp value)
Or
datetime.fromtimestamp(timestamp object)
The “datetime” is a python module to convert into a date, date-time, timestamp, etc. The “fromtimestamp” method helps to convert into a date. The “timestamp value” is an encoded sequence of the information to convert into a date. The python installed the datetime module by default. You do not need to install third software for conversion. The datetime uses either object or value inside of the “fromtimestamp” method.
The syntax of the timestamp to date conversion shows below:
datetime.fromtimestamp(objtmstmp).strftime('%d - %m - %y')
The “datetime” is a python module to convert into date. The “strftime” function shows the only date as per requirement. The function uses to date, month, and time as per the required format.
How to Convert Timestamp to Date in Python?
Install python software or use online IDE for coding.
The following link helps to download python software.
Website Link: https://www.python.org/
Create a python file using the .py extension. Then, start to write python code.
Filename: main.py
Import the “datetime” file to start timestamp conversion into a date.
from datetime import datetime
Create an object and initialize the value of the timestamp.
objtmstmp = 14590157322
Use the ” fromtimestamp ()” method to place either data or object.
objectdate = datetime.fromtimestamp(objtmstmp)
Or
objectdate = datetime.fromtimestamp(14590157322)
Print the date after conversion of the timestamp.
print(" date" , objectdate)
If you require the type of date, then print the date type of the python.
print("type of date object =", type(objectdate))
Combine the working procedure of the timestamp to date in the python.
from datetime import datetime
objtmstmp = 14590157322
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Examples of Timestamp to Date in Python
Given below are the examples of Timestamp to Date in Python:
Example #1
The timestamp to date convert for the 1970 year example and output.
Code:
from datetime import datetime
objtmstmp = 100043
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #2
The timestamp to date convert for the 2000 year example and output.
Code:
from datetime import datetime
objtmstmp = 1000000000
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #3
The timestamp to date convert at the 1970 year example and output.
Code:
from datetime import datetime
objtmstmp = 2500000000
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #4
The timestamp to date convert with four-digit value example and output.
Code:
from datetime import datetime
objtmstmp = 1000
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #5
The timestamp to date convert with a five-digit value example and output.
Code:
from datetime import datetime
objtmstmp = 10005
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #6
The timestamp to date converts to change date example and output.
Code:
from datetime import datetime
objtmstmp = 1000641
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #7
The timestamp to date converts to change month example and output.
Code:
from datetime import datetime
objtmstmp = 10006416
objectdate = datetime.fromtimestamp (objtmstmp)
print ("timestamp to date conversion.")
print (" date" , objectdate)
print ("type of date object =", type (objectdate))
Output:
Example #8
The timestamp to date converts to change current date example and output.
Code:
import datetime;
print ("Display current time")
current_time = datetime.datetime.now()
print ("current time:-", current_time)
print ("Display timestamp")
time_stamp = current_time.timestamp()
print ("timestamp:-", time_stamp)
Output:
Example #9
The timestamp to date converts to change current date example and output.
Code:
from datetime import datetime
objtmstmp = 1500000
objectdate = datetime.fromtimestamp(objtmstmp).strftime('%d - %m - %y')
print ("timestamp to date conversion.")
print (" date (date - month - year):" , objectdate)
objectdate = datetime.fromtimestamp(objtmstmp).strftime('%y - %d - %m')
print ("timestamp to date conversion.")
print (" date (year - date - month):" , objectdate)
objectdate = datetime.fromtimestamp(objtmstmp).strftime('%m - %d - %y')
print ("timestamp to date conversion.")
print (" date (month - date - year ):" , objectdate)
Output:
Conclusion
It helps to save date and time effortlessly in the database. The web application shows the date from the timestamp value using minimum code. The timestamp to date stores date and time together without complexion. It is used for updates, search date, and time without lengthy code.
Recommended Articles
This is a guide to Timestamp to Date in Python. Here we discuss the introduction, how to convert Timestamp to date in python? And examples. You may also have a look at the following articles to learn more –