Unix Timestamp to datetime in Python

Python Python-DateTime

In this post, we are going to learn how to convert Unix Timestamp to datetime in Python with examples. First we will get current epoch in Python and then use this in our examples.

What is Python Epoch time?


The Unix time is also known as epoch time. It represents the time passed since Jan 1, 1970. In Python date is not a built-in datatype, the datetime Module is used to manipulate Date and Time.

To convert Python Unix epoch/timestamp to datetime, Let us understand how to get Unix current Timestamp/epoch.

How to get Python current Timestamp/epoch?


  • Get the current date and time using datetime.now() Method.
  • Get the Timestamp/epoch from datetime using timestamp() method.
  • Convert timestamp to timestamp milliseconds by multiple 1000

In Our all examples, we are using the hard code value of epoch time. If you need to use the dynamic value of the timestamp, then use the one that we have calculated in the given below example.

Some format code in Python

  • %Y : four-digit year
  • %y : two digit year
  • %m: Month
  • %d: date
  • %H : for hours
  • %M : for minutes
  • %S: for second
  • %f : for milliseconds

Program to Get Python current epoch/timestamp from datetime

from datetime import datetime

timestamp = datetime.now().timestamp()

print("current timestamp =", timestamp)
print("datetime to epoch millisecond:",timestamp*1000)

Output

current timestamp = 1627691314.993335
datetime to epoch millisecond: 1627691314993.335

1. Convert Python epoch to datetime


By using the datetime module function fromtimestamp() we are converting unix timestamp to datetime.

Printing the default format(“%Y-%m-%d %H:%M:%S”) datetime using print() method.

The datetime class strftime() method is used to convert datetime to a specific format as per need.

Program Example

from datetime import datetime



unix_timestamp = datetime.now().timestamp()

# Getting date and time in local time

datetime_obj = datetime.fromtimestamp(int(unix_timestamp))

print('DateTime default Format :',datetime_obj)

print('Custome formatted DateTime:',datetime_obj.strftime("%d.%m.%y %H:%M:%S"))



Output

DateTime default Format : 2021-07-31 06:04:52
31.07.21 06:04:52

2. Python epoch/timestamp to dateTime with UTC


In this example, the Datetime module’s utcfromtimestamp() function is used to get the epoch time to datetime with UTC.

Finaly,The datetime class strftime() method is used to convert datetime to specific format as per need.

Program to epoch time to dateTime with UTC

from datetime import datetime

unix_timestamp = "1627671140"

# Getting the date and time in UTC
datetime_obj = datetime.utcfromtimestamp(int(unix_timestamp))
print(datetime_obj.strftime("%d.%m.%y %H:%M:%S"))

Output

30.07.21 18:52:20

3. Python epoch/Timestamp milleseconds to Datetime


In this example, we are repeating the same steps as mentioned in the above examples. We are converting Python epoch milliseconds to Datetime.

Program Example

#Python 3  Progaram to epoch milleseconds to Datetime millesecods

import datetime

millseconds = 1627673330000/ 1000.0
datetime_obj = datetime.datetime.fromtimestamp(millseconds).strftime('%Y-%m-%d %H:%M:%S.%f')

print(datetime_obj)

Output

2021-07-31 00:58:50.000000

4. Python unix timestamp/epoch to datetime string


Using Datetime module fromtimestamp() function to convert epoch time/Unix timestamp to datetime and strftime() function to convert datetime to string.

Program Example

import datetime

unix_time = 1627675321
datetime_obj = datetime.datetime.fromtimestamp(unix_time ).strftime('%Y-%m-%d %H:%M:%S')

print(datetime_obj)
print(type(datetime_obj))

Output

2021-07-31 01:32:01
<class 'str'>

5. Time Module to Unix timestamp to datetime string


The Python time module strftime() function is used in this example to convert unix timestamp/epoch to datetime.

The first parameter to this function is format code and another is the Unix timestamp.

Program Example

#Python3 program to convert unix timestamp to datetime string

import time

time_epoch = 1627675321

time_obj = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_epoch))

print("epoch to datetime string:", time_obj)

print(type(time_obj))



Output

epoch to datetime string: 2021-07-31 01:32:01
<class 'str'>

6. Unix timestamp to datetime Python Pandas


In this example, we are using the Pandas module to convert Unix timestamp to datetime.

First, import the panda’s module using import pandas as pd. Then using the pandas to_datetime() function to convert Unix timestamp/epoch to datetime. Finally printing the result using the print() method.

Program Example

import pandas as pd

time_epoch = 1627675321

print('unix timestamp to datetime:',pd.to_datetime(time_epoch, unit='s'))

Output

unix timestamp to datetime : 2021-07-30 20:02:01

Summary

  • In this post, we have learned how to convert Unix Timestamp to datetime in Python.First we get the current epoch/timestamp .
  • Then explained with different ways that includes Convert Python epoch to datetime,Python epoch/Timestamp milleseconds to Datetime milliseconds,Python unix timestamp/epoch to datetime string using datetime module fromtimestamp() and strftime() method to datetime.