10

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?

Steve K
  • 10,879
  • 4
  • 39
  • 39
alwbtc
  • 28,057
  • 62
  • 134
  • 188

1 Answers1

19

Just use

from datetime import timedelta

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
Marioanzas
  • 1,663
  • 2
  • 10
  • 33
Steve K
  • 10,879
  • 4
  • 39
  • 39
  • 1
    Sir, `43880.6543` is actually an excel date, it is represented this way when you change cell's format to `General`. Could you plewase revise your answer? – alwbtc Nov 01 '16 at 13:41