Convert Python datetime to epoch - GeeksforGeeks

23 Aug, 2021
In this article, we are going to discuss various ways by which we can convert Python DateTime to epoch. The epoch time is also known as POSIX time which will indicate the number of seconds passed from January 1, 1970, 00:00:00 (UTC) in most windows and Unix systems.
Note: Epoch is platform-dependent which means it depends on the system or operating system you are using.
DateTime is the time which is in the given format
year/month/day/hours/minutes/seconds:milliseconds
strftime() is used to convert string DateTime to DateTime. It is also used to convert DateTime to epoch. We can get epoch from DateTime from strftime().
Syntax: datetime.datetime(timestamp).strftime(‘%s’)
Parameter:
timestamp is the input datetime
$s is used to get the epoch string
datetime is the module
The parameter %s is a platform dependent format code, it works on Linux. In windows, the same code can be modified to run by %S in place of %s.
Example: Python code to convert datetime to epoch using strftime
Python3
# import datetime moduleimport datetime  # convert datetime to epoch using strftime from# time stamp 2021/7/7/1/2/1# for linux:epoch = datetime.datetime(2021, 7, 7, 1, 2, 1).strftime('%s')# for windows:# epoch = datetime.datetime(2021, 7,7 , 1,2,1).strftime('%S')print(epoch)  # convert datetime to epoch using strftime from# time stamp 2021/3/3/4/3/4epoch = datetime.datetime(2021, 3, 3, 4, 3, 4).strftime('%s')print(epoch)  # convert datetime to epoch using strftime from# time stamp 2021/7/7/12/12/34epoch = datetime.datetime(2021, 7, 7, 12, 12, 34).strftime('%s')print(epoch)  # convert datetime to epoch using strftime from # time stamp 2021/7/7/12/56/00epoch = datetime.datetime(2021, 7, 7, 12, 56, 0).strftime('%s')print(epoch)
1625619721
1614744184
1625659954
1625662560

we can get epoch from DateTime using timestamp().
Syntax: datetime.datetime(timestamp).timestamp()
Parameter:
datetime is the module
timestamp is the input datetime
Example: Python code to convert  DateTime to epoch using timestamp()
Python3
# import datetime moduleimport datetime  # convert datetime to epoch using timestamp()# from time stamp 2021/7/7/0/0/0epoch = datetime.datetime(2021, 7, 7, 0, 0, 0).timestamp()print(epoch)    # convert datetime to epoch using timestamp()# from time stamp 2021/3/3/4/3/4epoch = datetime.datetime(2021, 3, 3, 4, 3, 4).timestamp()print(epoch)  # convert datetime to epoch using timestamp()# from time stamp 2021/7/7/12/12/34epoch = datetime.datetime(2021, 7, 7, 12, 12, 34).timestamp()print(epoch)  # convert datetime to epoch using timestamp()# from time stamp 2021/7/7/12/56/00epoch = datetime.datetime(2021, 7, 7, 12, 56, 00).timestamp()print(epoch)
1625616000.0
1614744184.0
1625659954.0
1625662560.0

Picked
Python datetime-program
Python-datetime
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()