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) print(t) d = dateutil.parser.parse(t) print(type(d)) print(d)
Run this code, we will see:
00:01:40.300 <class 'datetime.datetime'> 2023-06-07 00:01:40.300000