Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert Python datetime to epoch

  • Last Updated : 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

DateTime is the time which is in the given format



year/month/day/hours/minutes/seconds:milliseconds

Method 1: Using strftime()

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 module
import 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/4
epoch = 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/34
epoch = 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/00
epoch = datetime.datetime(2021, 7, 7, 12, 56, 0).strftime('%s')
print(epoch)
Output
1625619721
1614744184
1625659954
1625662560

Method 2 : Using timestamp()

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 module
import datetime
  
# convert datetime to epoch using timestamp()
# from time stamp 2021/7/7/0/0/0
epoch = 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/4
epoch = 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/34
epoch = 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/00
epoch = datetime.datetime(2021, 7, 7, 12, 56, 00).timestamp()
print(epoch)
Output
1625616000.0
1614744184.0
1625659954.0
1625662560.0



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!