Python epoch to DateTime + Examples

In this python tutorial, you will learn about Python epoch to DateTime with a few examples. Python epoch is used to convert epoch to date and time. Here we will check:

  • Python DateTime to epoch time
  • python epoch to datetime string
  • python epoch to DateTime milliseconds
  • python epoch to DateTime timezone
  • python Unix epoch to DateTime
  • python epoch DateTime to milliseconds
  • python epoch timestamp to datetime
  • python epoch datetime year is out of range

What is Python epoch

The epoch time is also called Unix time, POSIX time, and Unix timestamp. The epoch time means the number of seconds that have passed since January 1 1970 excluding leap seconds. The Unix time has 10 digits. Unix time can represent all timezone at once.

Python epoch to DateTime

Let us see with the below example on Python epoch to DateTime.

Example:

import datetime
epochtime = 30256871
datetime = datetime.datetime.fromtimestamp(epochtime)
print(datetime)

In this example I have imported the datetime package and taken the variable as epoch time and value to the variable like the number of seconds and used datetime.datetime.fromtimestamp(epochtime) method to convert the epoch time to DateTime.

Below image shows the output:

Python epoch to DateTime

Python DateTime to epoch time

Now, let us see how to get epoch time from Python DateTime.

The below example, I have passed the datetime and it returns the epoch time.

Example:

import datetime
timedate = datetime.datetime(2020, 12, 14, 0, 0).timestamp()
print(timedate)

In this example. I used timestamp function.

Below image shows the output:

Python DateTime to epoch time
Python DateTime to epoch time

Python epoch to datetime string

Let us see an example where, we will pass the epoch time and it will give us the datetime in string format in Python.

Example:

import datetime
ts = datetime.datetime.fromtimestamp(800025)
print(ts.strftime('%Y-%m-%d %H:%M:%S'))

In this example to convert epoch to date time, I have taken fromtimestamp() function from datetime module to get a date from the Unix timestamp.

This fromtimestamp() function takes timestamp as input and gives DateTime as output. The strftime is a string that returns date and time in string format.

We use Python strftime() to convert a date and time to string.

  • %Y indicates the year
  • %m indicates the month
  • %d indicates the day
  • %H indicates an hour
  • %M indicates the month
  • %S indicates seconds

Below image shows the output.

Python epoch to datetime string
Python epoch to datetime string

Python epoch to DateTime milliseconds

Now, let us see how to get date time with miliseconds from epoch time in Python.

Example:

import datetime

millseconds = 984567.224325
datetime = datetime.datetime.fromtimestamp(millseconds)

print(datetime)

Here I have taken fromtimestamp function to covert epoch with milliseconds format in Python.

Below image shows the output.

Python epoch to DateTime milliseconds
Python epoch to DateTime milliseconds

Python epoch to DateTime timezone

from datetime import datetime
import pytz
tz = pytz.timezone('CST6CDT')
datetz = tz.localize(datetime(2020, 11, 17, 1, 37, 50), is_dst=None)
print(datetz)

In this example, pytz package is imported. To get the current date for the given time zone tz = pytz.timezone(‘CST6CDT’) is used.

To get beginning of the current day in given timezone as a DateTime with timezone datetz = tz.localize(datetime(2020, 11, 17, 1, 37, 50), is_dst=None) is_dst=None is a flag passed to localize.

See below:

Python epoch to DateTime timezone
Python epoch to DateTime timezone

Python Unix epoch to DateTime

This is same like the above example, where we are passing Unix epoch time and it gave us in DateTime format.

Example:

import datetime
print(datetime.datetime.fromtimestamp(int("258741369")).strftime('%Y-%m-%d %H:%M:%S'))

In this example. I have taken strftime() which is a string method that returns date and time.

Below image shows the output.

Python Unix epoch to DateTime

Python epoch DateTime to milliseconds

Now, let us see how to get milliseconds from Python DateTime in epoch format.

from datetime import datetime

date_obj = datetime.strptime('14.12.2020 12:37:45,72',
                           '%d.%m.%Y %H:%M:%S,%f')
milliseconds = date_obj.timestamp() * 1000

print(milliseconds)

Ideally, the date_obj.timestamp(), will return the date in seconds. To get in milliseconds, we need to multiply by 1000.

Below image shows the output.

Python epoch DateTime to milliseconds
Python epoch DateTime to milliseconds

Python epoch timestamp to datetime

Let us see how to convert epoch timestamp to datetime.

Example:

import datetime
epochtime = 30256871
datetime = datetime.datetime.fromtimestamp(epochtime)
print(datetime)

Below image shows the output:

Python epoch timestamp to datetime
Python epoch timestamp to datetime

Python epoch datetime year is out of range/Python Invalid argument

While working with Python epoch datetime, I got the below error, that says, Python epoch datetime year is out of range or Python Invalid argument.

Example:

import datetime
timedate = datetime.datetime(1950, 12, 14, 0, 0).timestamp()
print(timedate)

In this example I have taken the year 1950 which is not in the range of timestamp. So for this output will be an error which is shown in the below image as Invalid argument.

year out of range

To fix the Invalid argument error, change the date from 1950 to anything after 1970.

You may like the following Python tutorials:

Here, we learned about how to convert Python epoch to DateTime.