Skip to content
Related Articles
Open in App
Not now

Related Articles

Convert Python datetime to epoch

Improve Article
Save Article
  • Last Updated : 15 Sep, 2022
Improve Article
Save Article

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

Using calendar module to convert Python datetime to epoch

Here we are using the calendar module to convert datetime to epoch using the timetuple() function.

Python3




# importing the required module
import datetime
import calendar
 
t=datetime.datetime(1971, 1, 1, 0, 0, 0)
print(calendar.timegm(t.timetuple()))
 
t=datetime.datetime(2021, 7, 7, 1, 2, 1)
print(calendar.timegm(t.timetuple()))

Output:

31536000
1625619721

Using strftime() to convert Python datetime to epoch

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
 
# 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:

1625599921
1614724384
1625640154
1625642760

Using timestamp() to convert Python datetime to epoch

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:

1625596200.0
1614724384.0
1625640154.0
1625642760.0

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!