Convert Epoch to Datetime in Python

Convert Epoch to Datetime in Python

Sep 16, 2024
454

image

Epoch is time representation standard that is used on Unix timestamp which every digit represents every second happens from January 1st, 1970 until today. Epoch time contain 10 digit number and represented every time zone with added 3600 seconds. We will sometime have to work with this Epoch time when we handle data in logs and databases.

1. What you need to do before do this tutorial?


  1. Have Python programming language installed on your PC.

  2. Make sure Python is included in path Command Line

💡 Tips

If you don't want install Python on your computer, you can use Google Colaboratory and code in your browser.

2. Convert Epoch to Datetime in Python


  1. Import Datetime Library

    First we need to import datetime library in Python so we can use datetime class with all the function in it.

    📝 Code

    TODO : Import datetime library

    from datetime import datetime

  2. Convert using fromtimestamp function

    📝 Code

    TODO : Convert Epoch to Datetime

    epochTime = 1676106378

    currentTime = datetime.fromtimestamp( epochTime )

    print('1. Current Epoch to Datetime : ',currentTime)

The currentTime will return date with datetime data type and that's how you convert Epoch to Datetime in Python.

Подобається цей допис?

Купити для vincentwidyan | Erin Data Studio каву

Більше від vincentwidyan | Erin Data Studio

КонфіденційністьУмови