Open In App

Convert Python datetime to epoch

Last Updated : 25 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 a 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

Converting epoch time into DateTime using fromtimestamp

The code turns an epoch time (the number of seconds since January 1, 1970) into a DateTime format that can be read. It converts the epoch time to a DateTime object using the datetime.fromtimestamp() function. Following that, the original epoch time and the converted DateTime are printed.

Python3




import datetime 
   
# given epoch time 
epoch = 33456871 
   
# using the datetime.fromtimestamp() function 
epoch_date_time = datetime.datetime.fromtimestamp( epoch ) 
   
print("Given epoch time:", epoch) 
print("Converted Datetime:", epoch_date_time )


Output:

Given epoch time: 33456871
Converted Datetime: 1971-01-23 11:04:31

Convert Python DateTime to epoch using strftime()

strftime() is used to convert the 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

Converting epoch time into DateTime using pytz Python Module

The code localizes a DateTime object to the Central Standard Time time zone. The code returns a DateTime object that is aware of the CST time zone. The DateTime object is 5 hours behind Universal Time (UTC).

Python3




# importing the required package(s) and module(s) 
from datetime import datetime 
import pytz 
   
# using the timezone() function 
zone = pytz.timezone('CST6CDT'
   
# using the localize() function 
date_zone = zone.localize( datetime( 2022, 5, 16,
                         9, 15, 32 ), is_dst = None
   
# printing the values 
print( "DateTime Time zone:", date_zone )


Output:

DateTime Time zone: 2022-05-16 09:15:32-05:00

Convert Python DateTime to epoch 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:

1625596200.0
1614724384.0
1625640154.0
1625642760.0


Previous Article
Next Article

Similar Reads

Convert Epoch Time to Date Time in Python
Epoch time, also known as Unix time or POSIX time, is a way of representing time as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970. Converting epoch time to a human-readable date and time is a common task in programming, especially in Python. In this article, we will explore some si
3 min read
How to convert a Python datetime.datetime to excel serial date number
This article will discuss the conversion of a python datetime.datetime to an excel serial date number. The Excel "serial date" format is actually the number of days since 1900-01-00. The strftime() function is used to convert date and time objects to their string representation. It takes one or more inputs of formatted code and returns the string r
3 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
How to Fix - "datetime.datetime not JSON serializable" in Python?
In this article, we are going to learn how to fix the error "datetime.datetime not JSON serializable" in Python. datetime.datetime is a class in the Python datetime module that represents a single point in time. This class is not natively supported by the JSON (JavaScript Object Notation) format, which means that we cannot serialize a datetime.date
4 min read
How to convert timestamp string to datetime object in Python?
Python has a module named Datetime to work with dates and times. We did not need to install it separately. It is pre-installed with the Python package itself. A UNIX timestamp is several seconds between a particular date and January 1, 1970, at UTC. Input: "2023-07-21 10:30:45"Output: 2023-07-21 10:30:45Explanation: In This, We are converting a tim
2 min read
How to convert DateTime to integer in Python
Python provides a module called DateTime to perform all the operations related to date and time. It has a rich set of functions used to perform almost all the operations that deal with time. It needs to be imported first to use the functions and it comes along with python, so no need to install it separately. Here, we deal with a special date objec
2 min read
How to convert Python's .isoformat() string back into datetime object
In this article, we will learn how to convert Pythons .isoFormat() string back into a datetime object. Here we are using the current time and for that, we will store the current time in the current_time variable. The function now() of this module, does the job perfectly. Example: Getting current time C/C++ Code from datetime import datetime current
1 min read
Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python
In this article, we are going to convert the DateTime string into the %Y-%m-%d-%H:%M:%S format. For this task strptime() and strftime() function is used. strptime() is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds Syntax: datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") strftime() is used t
2 min read
How to Convert DateTime to UNIX Timestamp in Python ?
The Unix timestamp is a single signed integer that grows by one every second, allowing computers to store and manipulate conventional date systems. The software is then translated into a human-readable format. The Unix timestamp is the number of seconds calculated since January 1, 1970. In this article, we are going to see how to convert DateTime t
5 min read
How to convert datetime to date in Python
In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date from Pandas in Python. Method 1: Convert DateTime t
3 min read
Practice Tags :
three90RightbarBannerImg