Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert float to timedelta seconds in python?

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?

like image 849
alwbtc Avatar asked Aug 23 '14 05:08

alwbtc


1 Answers

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
like image 151
Steve K Avatar answered Sep 28 '22 08:09

Steve K