It’s rather typical in databases to store date and time as a Python timestamp while working with programs.
A timestamp is a piece of encoded information commonly used in UNIX that identifies the date and time of an occurrence. This data could be accurate to the millisecond. It is the POSIX timestamp for the datetime instance.
A timestamp is a series of characters or encoded information that is used to determine when a specific event occurred, typically providing the date and time of day, accurate to a fraction of a second.
Converting timestamp to datetime
While the timestamp’s normal format is simply a floating-point number, there are situations when it will be represented in ISO 8601 format. We may use the datetime module’s fromtimestamp() method to convert the timestamp back to a datetime object.
The syntax is as follows:
datetime.fromtimestamp(timestamp, tz = None)
It returns the POSIX timestamp corresponding to the local date and time, as returned by time.time(). If the optional argument ‘tz’ is None or not given, the timestamp is transformed to the platform’s local date and time, and the datetime object returned is naive.
Example 1: Python timestamp to datetime
In the program below, we have made use of the Python fromtimestamp() method. The fromtimestamp() function is used to return the date associated with a given timestamp. The date class’s function fromtimestamp() computes and returns the date and time corresponding to a specified timestamp. The timestamps in this example range from 1970 to 2038.
Example
# Python program to illustrate fromtimestamp()
from datetime import datetime
timestamp = 1549836078
# converting timestamp to date
dt_object = datetime.fromtimestamp(timestamp)
print('Date and Time is:', dt_object)
Output
Date and Time is: 2019-02-10 22:01:18
We’ve imported the datetime class from the datetime module here. Then we applied datetime.fromtimestamp() class method that returns the current date and time (datetime object). This object is kept in the dt_object variable.
Converting datetime to timestamp
A datetime module’s timestamp() method returns the POSIX timestamp corresponding to the datetime instance. The type of return value is float. The following steps are taken to convert a datetime to a timestamp:
- To begin, we use the datetime.now() function in Python to obtain the current date and time.
- Then, to the datetime, we pass the current datetime.timestamp() function to obtain the UNIX timestamp.
Example 2: Python datetime to timestamp
Example
from datetime import datetime
# current date and time
now = datetime.now()
print('Date and Time is:', now)
timestamp = datetime.timestamp(now)
print("timestamp =", timestamp)
Output
Date and Time is: 2021-09-28 18:48:14.595247
timestamp = 1632854894.595247
Frequently Asked Questions
Q1. What is a Python timestamp?
A timestamp is a piece of encoded information commonly used in UNIX that identifies the date and time of an occurrence. This data could be accurate to the millisecond. It is the POSIX timestamp for the datetime instance.
A timestamp is a series of characters or encoded information that is used to determine when a specific event occurred, typically providing the date and time of day, accurate to a fraction of a second.
Q2. How do you get a timestamp in Python?
There are many ways in which we can acquire the timestamp in Python. Few methods are:
- Using the time module – The time() method of the time module returns the current time in timestamp format, which is simply the time elapsed since the epoch time, January 1, 1970.
Example
import time
# current timestamp
x = time.time()
print('Timestamp:', x)
Output
Timestamp: 1632858751.3295631
- Using the datetime module – Datetime module provides classes for managing date and time in a more object-oriented manner. One of them is the datetime.now() function that returns the number of seconds since the epoch.
Example
from datetime import datetime
timestamp = datetime.now().timestamp()
print('Timestamp:', timestamp)
Output
Timestamp: 1632858851.842539
- By using the calendar module – To convert the current time to a timestamp, use the calendar module’s calendar.timegm() method.
To begin, we import both the time and calendar modules. Then, using the time module’s time.gmtime() method, obtain the GMT time. Finally, use the calendar.timegm() method to obtain a timestamp.
Example
import calendar
import time
timestamp = calendar.timegm(time.gmtime())
print('Timestamp:', timestamp)
Output
Timestamp: 1632859023
Leave a Reply