How to convert float to timedelta seconds in python?

alwbtc picture alwbtc · Aug 23, 2014 · Viewed 9.2k times · Source

How do you instantiate a datetime.timedelta to a number of seconds in python? For example, 43880.6543

Edit (as per clarification by OP): I have an Excel sheet, where a cell shows 43880.6543 (when the format is Generic) instead of a date. How do I convert this value to a datetime in python?

Answer

Steve K picture Steve K · Aug 23, 2014

Just use

value = timedelta(seconds=43880.6543)

You can check the value is ok with

value.total_seconds()

Edit: If, in an Excel spreadsheet, you have a date expressed as a number, you can use python to convert that number into a datetime by doing this:

value = datetime(1900, 1, 1) + timedelta(days=43880.6543)
# value will be February 2nd, 2020 in the afternoon