python


Timestamp to Datetime in Python


In this tutorial, we will learn about how to convert timestamp to datetime objects as well as datetime to timestamp.You will get them 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.

Timestamp to Datetime

Example of timestamp to datetime:

example of coversion

Output:

output

We can see in the above code we used datetime module to convert timestamp in datetime.We used datetime.fromtimestamp() function which helped us to decode the timestam.After decoding we assigned it to date_object an alo print them by using print statement.

Datetime to Timestamp

Example of datetime to timestamp:

example of conversion

output

We can check that the output of the program is same as the input we provided as timestamp in the first program.


We can see in above code how to convert datetime to timestamp. We have to provide a date in the format datetime() and this will help us to know the timestamp.

Timestamp of current time

You can also find the timestamp of the current time without using datetime method

. Example:

example of current time conversion

You can see in the above code that we used datetime.now() method to access the current time and the other process is same as previous one.