In this lesson we want to learn How to Convert DateTime to UNIX Timestamp in Python.
What is UNIX Timestamp ?
UNIX timestamp is way of representing a point in time as the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC (Coordinated Universal Time). this specific point in time is known as the “epoch” and serves as the reference point for all UNIX timestamps.
UNIX timestamps are widely used in computer systems and applications as they provide simple and efficient way of representing and comparing dates and times. they are also easy to work with and convert to other date and time formats, making them a popular choice for many applications.
UNIX timestamp is simple integer that represents the number of seconds that have elapsed since epoch. it is commonly used in web development as well as in other areas where it is necessary to store and manipulate dates and times in a machine-readable format.
How to Convert DateTime to UNIX Timestamp in Python ?
In Python you can convert a datetime object to UNIX timestamp using timestamp method from the datetime module. UNIX timestamp is the number of seconds that have elapsed since January 1, 1970 (the “epoch”). Here’s an example:
from datetime import datetime
date_object = datetime.now()
timestamp = date_object.timestamp()
print("UNIX timestamp:", timestamp)
UNIX timestamp: 1675320270.912822
In this example, the datetime.now function is used to get the current date and time, and the timestamp method is used to convert the resulting datetime object to a UNIX timestamp. The UNIX timestamp is then printed to the console.