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 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 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 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 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 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 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 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 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.
To fix the Invalid argument error, change the date from 1950 to anything after 1970.
You may like the following Python tutorials:
- Python write variable to file + Examples
- Python 3 pickle typeerror a bytes-like object is required not ‘str’
- Python binary search and linear search
- Python dot product and cross product
- Python exit command (quit(), exit(), sys.exit())
- Python input and raw_input function
- Sorting algorithms in Python
- Working with JSON data in Python
- Python write String to a file
- Python write list to file
- How to Create a Snake game in Python using Turtle
- How to convert dictionary to JSON in Python
Here, we learned about how to convert Python epoch to DateTime.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.