Python Convert Float Seconds to DateTime Object – Python Tutorial

By | June 7, 2023

It is easy to convert a float seconds to python datetime. In this tutorial, we will use an example to show you how to convert.

Step 1: convert float senconds to string datetime format

For example, 100.3 seconds = 00:01:40.300

We can use sec2time() function to covert.

Here is the tutorial:

Python Convert Seconds to Days, Hours, Minutes and Seconds – Python Tutorial

Step 2: convert string datetime to python datetime object

We can use dateutil.parser.parse() function to do.

Here is the tutorial:

Python Detect Datetime String Format and Convert to Different String Format

Finally, we can convert a float seconds to python datetime object.

For example:

import dateutil.parser

seconds = 100.3
t = sec2time(seconds)
d = dateutil.parser.parse(t)

Run this code, we will see:

<class 'datetime.datetime'>
2023-06-07 00:01:40.300000