简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM