In this article, we are going to learn how we can convert data time object to Epoch. We will make use of the datetime module of Python to get the current date and time. Once we have it then we will convert it to a Epoch by using the timestamp function.
What is Epoch?
Epoch, also known as Unix Time is method of date and time representation. It represents time by the number of seconds that have elapsed since 00:00:00 UTC on 1 January 1970,which is considered as the beginning of the Unix epoch.
Environment and Requirement:-
Python Program to Convert Datetime to Epoch
from datetime import datetime d = datetime(2022,12,28) print(d.timestamp())
- Import datetime class from datetime module.
- Used “d” as an object of datetime class in which datetime(local date) is stored.
- Then used print command with obj.timestamp() .
- Timestamp is the date and time of occurrence of an event to an accuracy of milliseconds.
It returns the time elapsed from the epoch time which is set to 00:00:00 UTC for 1 January 1970.