How do i convert a timestamp in python?

In this article, you will learn to convert timestamp to datetime object and datetime object to timestamp (with the help of examples).

It's pretty common to store date and time as a timestamp in a database. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC.


Example 1: Python timestamp to datetime

from datetime import datetime

timestamp = 1545730073
dt_object = datetime.fromtimestamp(timestamp)

print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))

When you run the program, the output will be:

dt_object = 2018-12-25 09:27:53
type(dt_object) = 

Here, we have imported datetime class from the datetime module. Then, we used datetime.fromtimestamp() classmethod which returns the local date and time (datetime object). This object is stored in dt_object variable.

Note: You can easily create a string representing date and time from a datetime object using strftime() method.


Example 2: Python datetime to timestamp

You can get timestamp from a datetime object using datetime.timestamp() method.

from datetime import datetime

# current date and time
now = datetime.now()

timestamp = datetime.timestamp(now)
print("timestamp =", timestamp)

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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:

How do i convert a timestamp in python?

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.

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 –

  1. Shell sort in Python
  2. Insertion sort in Python
  3. Flask in Python
  4. Python List extend

How do I convert a date time stamp to a date in Python?

Import the “datetime” file to start timestamp conversion into a date. Create an object and initialize the value of the timestamp. Use the ” fromtimestamp ()” method to place either data or object. Print the date after conversion of the timestamp.

How do I format a timestamp in Python?

Some of the most useful directives are:.
%d for day of month as a zero-padded decimal like 01, 02, 03..
%m for month as a zero-padded decimal number..
%Y for year with century as a decimal number..
%H for 24 hour clock with a zero-padded hour value..
%M for zero-padded minutes, and..
%S for zero-padded seconds..

How do I convert a timestamp to a number in Python?

Convert the DateTime object into timestamp using DateTime. timestamp() method. We will get the timestamp in seconds. And then round off the timestamp and explicitly typecast the floating-point number into an integer to get the integer timestamp in seconds.

How do I convert a timestamp to a string in Python?

Python : How to convert datetime object to string using datetime..
timestampStr = dateTimeObj. strftime("%d-%b-%Y (%H:%M:%S.%f)") ... .
dateTimeObj = datetime. now() ... .
dateStr = dateTimeObj. strftime("%d %b %Y ") ... .
timeStr = dateTimeObj. strftime("%H:%M:%S.%f") ... .
dateStr = dateTimeObj..