Javatpoint Logo
Javatpoint Logo

Python epoch to Datetime

In the following tutorial, we will understand the conversion of epoch to DateTime in the Python programming language using some examples. We will use Python epoch in order to convert epoch to date and time, respectively. We will also cover the following topics:

  1. Converting DateTime into epoch time
  2. Converting epoch into DateTime string
  3. Converting epoch into DateTime milliseconds
  4. Converting epoch into DateTime timezone
  5. Converting Unix epoch into DateTime
  6. Converting DateTime into epoch time in milliseconds
  7. Converting epoch timestamp to DateTime
  8. Python epoch DateTime year is out of range

But before we get started, let us understand epoch time in Python in brief.

What is epoch time in Python?

Epoch time, also called Unix time or POSIX time, is a framework for following time as various seconds that have passed since the Unix age, which is 12 PM UTC on January 1, 1970. This framework is broadly utilized in figuring frameworks and programming dialects, including Python.

In Python, we can work with age time utilizing the time module, which gives capabilities to controlling time-related values. One of the essential capabilities for managing age time is time.time(), which returns the ongoing time in seconds since the age as a drifting point number.

Converting epoch time into DateTime using Python

We can use the Python datetime.fromtimestamp() function in order to convert the epoch time into DateTime. Let us consider the following example demonstrating the conversion of the Python epoch into DateTime.

Example:

Output:

Given epoch time: 40246871
Converted Datetime: 1971-04-12 01:11:11

Explanation:

In the above example, the datetime module is imported, taking into account the control of date and time values. The variable epoch_time is then doled out the particular worth of 40246871, addressing the slipped by seconds since the Unix age (January 1, 1970, 00:00:00 UTC).

The core usefulness of the content lies in the use of the datetime.fromtimestamp() capability. This capability requires some investment (epoch_time) and changes it into a more comprehensible configuration by making a datetime object. The subsequent datetime object, addressing the same date and time, is put away in the variable date_time.

To give perceivability into the change, the content incorporates two print() proclamations. The first, print("Given age time:", epoch_time), shows the first age time esteem, while the subsequent one, print("Converted Datetime:", date_time), yields the changed over datetime value.

Converting DateTime into epoch time using Python

In order to convert DateTime into epoch time, we will use the timestamp() function. First, let us consider the following example illustrating the conversion of DateTime into epoch time.

Example:

Output:

Converted epoch time: 1623184200.0

Explanation:

In the above example, the datetime module is imported for dealing with date and time-related activities. The content exhibits the change of a particular datetime into age time utilizing the timestamp() capability.

The interaction starts by making a datetime object addressing June 9, 2021, at 2:00 AM. This datetime object is then passed as a contention to the timestamp() capability, which returns the same age time (the quantity of seconds that have slipped by since the Unix age, January 1, 1970, 00:00:00 UTC). The subsequent age time is put away in the variable epoch_time.

The content finishes up with a print() proclamation: print("Converted age time:", epoch_time), which yields the determined age time for the predefined datetime. Converting epoch time into DateTime string using Python In order to convert the epoch time into datetime string, we need to convert the epoch time into datetime and then pass the resultant datetime variable to the strftime() function to convert it into datetime string successfully.

Example:

Output:

Given epoch time: 700024
Converted datetime string: 1970 - 01 - 09  07 : 57 : 04

Explanation:

In the above example, we have again imported the datetime library. We have then used the datetime.fromtimestamp() function in order to convert a given epoch time into datetime and then pass it to the strftime() function to convert the datetime object into a datetime string. The strftime() function parameters are the format codes in which we wanted to customize the string.

  1. %Y designates the year
  2. %m designates the month
  3. %d designates the day
  4. %H designates an hour
  5. %M designates the minute
  6. %S designates seconds

Converting the epoch time into DateTime milliseconds using Python

We can get the DateTime along with milliseconds from the epoch time using the method we did for the conversion of epoch time into DateTime. Let us consider the following example demonstrating the same.

Example:

Output:

Given epoch time: 402471.238201
Converted Datetime: 1970-01-05 21:17:51.238201

Explanation:

In the above example, the datetime module is imported to work with the treatment of date and time-related activities. The basic role of the content is to change over a given age time, determined as 402471.238201 seconds, into an intelligible configuration utilizing both a datetime object and an organized string.

The content returns to utilize the datetime.fromtimestamp() capability to change over the age time into a datetime object. The subsequent datetime object, put away in the variable date_time, exemplifies the comparing date and time subtleties. At last, the content prints both the first age time and the changed over datetime object.

These print() proclamations produce yield showing both the crude age time and the organized datetime object, giving a reasonable portrayal of the first timestamp and its human-accommodating same.

Converting epoch time into DateTime time zone using Python

We can use the timezone() function of the pytz package in order to convert epoch time into the DateTime time zone. Let us consider the following example demonstrating the conversion of same.

Example:

Output:

DateTime Time zone: 2021-06-10 09:34:32-05:00

Explanation:

In the above example, the datetime module is utilized alongside the pytz module to work with date and time objects in a particular time region. The essential goal of the content is to make a datetime object and expressly put down its point in time zone to 'CST6CDT' (Focal Standard Time with Sunlight Saving Time), then, at that point, print the subsequent date and time.

In this manner, the timezone() capability from the pytz module is utilized to make a time region object addressing 'CST6CDT'. Then, the restrict() capability from the time region object is used to establish the point in time zone for a particular datetime object (June 10, 2021, 9:34:32)

The is_dst=None boundary shows that the light saving time (DST) data isn't unequivocally given, permitting the restrict() capability to decide the suitable DST status in view of the predetermined time. At long last, the content prints the subsequent datetime object with the expressly set time region.

Converting Unix epoch time into DateTime using Python

This process of converting the Unix epoch time into DateTime is similar to what we did earlier. This process involves the datetime.fromtimestamp() function to convert the Unix epoch time into DateTime object and uses the strftime() function to transform the object into the suitable DateTime format.

Let us consider the following example demonstrating the same.

Example:

Output:

Unix epoch time: 252384207
DateTime: 1977 - 12 - 31  08 : 13 : 27

Explanation:

In the above example, the datetime module is used to change over a given age time, indicated as 252384207 seconds, into a more intelligible organization. The essential objective is to make a datetime object from the age time and afterward design it as a string for show. The content starts by bringing in the datetime module. The datetime.fromtimestamp() capability is then used to change over the age time into a datetime object.

The subsequent datetime object, put away in the variable datetime_obj, typifies the relating date and time subtleties. Then, the strftime() capability is utilized to design the datetime object as a string with a particular organization. For this situation, the configuration indicated is "%Y - %m - %d %H : %M : %S," addressing the year, month, day, hour, moment, and second.

At long last, the content prints both the first Unix age time and the arranged datetime string. These print() proclamations create yield showing both the crude age time and the organized datetime string, giving an unmistakable portrayal of the first timestamp and its human-accommodating same.

Converting DateTime into epoch time in milliseconds using Python

In order to convert the epoch DateTime into milliseconds, we can use the strptime() function to return the datetime object from the specified string and use the timestamp() function to convert the object into seconds. Moreover, we need to multiply the resultant value by a thousand in order to produce the epoch DateTime in milliseconds.

Let us consider an example demonstrating the same.

Example:

Output:

epoch time in milliseconds: 1623301415720.0

Explanation:

In the above example, the datetime module is utilized to change over a designed string addressing a particular date and time into an age time esteem estimated in milliseconds. The cycle starts with the utilization of the strptime() capability, which parses the info string '10-6-2021 10:33:35,72' in light of the gave design '%d-%m-%Y %H:%M:%S,%f'. This configuration indicates the day, month, year, hour, moment, second, and microsecond parts of the information string.

The subsequent datetime object, put away in the variable dt_obj, epitomizes the parsed date and time data. In this manner, the content ascertains the age time by utilizing the timestamp() strategy for the datetime object, addressing the quantity of seconds that have passed since the Unix age (January 1, 1970, 00:00:00 UTC). Increasing this worth by 1000 believers it to milliseconds, giving a more granular portrayal of time.

At long last, the content prints the determined age time in milliseconds utilizing the print() articulation.

Converting epoch timestamp into DateTime using Python

We can use the datetime.fromtimestamp() function to convert the epoch timestamp into DateTime. Here is an example shown below:

Example:

Output:

epoch timestamp: 33456871
DateTime: 1971-01-23 11:04:31

Explanation:

In the above example, the datetime module is utilized to change over a given age timestamp, addressed as 33456871 seconds, into a more reasonable and comprehensible configuration. The content starts by bringing in the datetime module, giving admittance to different functionalities to working with dates and times.

The age timestamp, implying the quantity of seconds that have unfolded since the Unix age (January 1, 1970, 00:00:00 UTC), is allocated to the variable epoch_time inside the content. Thusly, the datetime.fromtimestamp() capability is used to change over this age timestamp into a datetime object named the_datetime.

The content closes by printing both the first age timestamp and the organized datetime object utilizing the print() articulations.

Understanding errors related to epoch datetime in Python

While working with the epoch datetime in the Python programming language, we might encounter an error saying Python epoch datetime year is out of range or Python Invalid Argument.

Let us consider an example in order to understand the problem.

Example:

Output:

Traceback (most recent call last):
  File "D:\Python\epochpython.py", line 5, in 
    epoch_time = datetime.datetime(1960, 6, 10, 0, 0).timestamp()
OSError: [Errno 22] Invalid argument

Explanation:

In the above example, the datetime module is utilized to change over a particular datetime, for this situation, '1960-06-10 00:00:00', into age time utilizing the timestamp() capability. The content starts by bringing in the datetime module, giving admittance to functionalities connected with date and time tasks.

The center usefulness of the content lies in the usage of the timestamp() capability, which is applied to a datetime object addressing June 10, 1960, at 12 PM. This capability works out and returns the age time, addressing the quantity of seconds passed since the Unix age (January 1, 1970, 00:00:00 UTC) until the predetermined datetime.

The outcome is then printed utilizing the print() proclamation, creating a result that could seem to be "- 306691200.0".







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA