Convert Unix timestamp to DateTime in python

Convert unix timestamp to datetime in Python

A UNIX timestamp is defined as the several numbers of seconds between a particular date and the Epoch i.e January 1, 1970 at UTC.

In this tutorial, we will see some methods by which we can convert a UNIX timestamp to DateTime in Python.

Using fromtimestamp() Function

DateTime module in python is used to deal with the date and time-related problems in python.

One of the functions in this module is the fromtimestamp() function. This function returns the time that is expressed as seconds i.e UNIX timestamp.

Example 1:

Output:

dateAndtime: 2019-08-02 12:13:43

Here, we first import the datetime class from the datetime module. Then we store the UNIX value object in a variable. After that, we use the datetime.fromtimestamp() function that will get us the time and date. Finally, we print the unix_val variable.

To get the current timestamp, we use the now() function from the datetime module.

Output:

current timestamp = 1621358136.138725

To convert this again to DateTime, we use the same code from example 1.

Using strftime() Function

Another function in the datetime module is the strftime() function. This function helps in returning the DateTime in a specific form. To convert the date and time objects to their string representation, this function is generally used.

Example:

Output:

Converted date and time: 2019-08-02 12:13:43

In the above code, %d, %m, %Y, %H, %M and %S are called format codes which represent days, month, year, hour, minutes, and seconds respectively. In this code also, fromtimestamp() is used to return the time that is expressing in the UNIX timestamp.

Using to_datetime() Function

This function comes under the Pandas library in python. Pandas is one of the most common libraries in python. This library helps in manipulating and analyze data and is widely used in the field of data science.

The to_datatime() of the Pandas library helps in converting a Unix timestamp to DateTime in Python.

Example:

Output:

2019-08-02 06:43:43

In this method, we first start by importing the Pandas which is pre-installed in python. Then we make a variable ‘timestamp’ to store the UNIX timestamp that has to converted. After that, we use the to_datetime function. Note that, in the argument of the to_datetime function, we have mentioned the unit equal to ‘s’. The unit variable represents the unit of the UNIX timestamp i.e seconds.

That’s all about how to convert UNIX timestamp to DateTime in Python.

Related Posts

  • Epoch to datetime in Python
    21 August

    Convert epoch to datetime in python

    Table of ContentsUsing the datetime.fromtimestamp() function to convert epoch to datetimeUsing the pandas.to_datetime() function to convert epoch to datetime in Python Epoch time refers to the time elapsed since time was started measuring on January 1, 1970. Usually, it is measured in seconds. It is also known as UNIX time. In this article, we will […]

  • Convert datetime to date in Python
    20 June

    Convert datetime to date in Python

    Table of ContentsUsing the date() function to convert datetime to date in Python.Using the dt.date function in pandas to convert datetime to date in Python. Date and time in Python are not a pre-defined data type itself. The datetime module can be imported to the Python code and it provides classes used that manipulate and […]

  • Last day of month in Python
    20 May

    Get last day of month in Python

    Table of ContentsUsing the calendar.monthrange() functionUsing the dateutil.relativedelta() functionUsing the datetime moduleUsing the pandas.end_time() functionUsing the pandas.date_range() In this article, we will discuss how to find the last day of the month in Python using different modules. Using the calendar.monthrange() function The monthrange() function is used to return a tuple containing the first weekday of […]

  • Add days to date in Python
    19 May

    Add days to date in Python

    Table of ContentsUsing datetime.timedelta() FunctionAdd one day to date using datetime.timedelta()Using timedelta method from datetime moduleUsing Pandas Module In python, we deal with a lot of date and time-related problems. There is no specific data type for date and time still there is a lot of manipulation done on them in python. We can perform […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.