1

I have this list of integers and need to convert it to date using python.

  • 42222 should be 8/6/2015
  • 42290 should be 10/13/2015
  • 42319 should be 11/11/2015

I get the equal date of the integer when i paste in to excel then format the cell to Date.

Liza
  • 808
  • 2
  • 13
  • 30

1 Answers1

4

Excel dates start counting around the year 1900. This will do it:

from datetime import datetime, timedelta

def xldate_to_datetime(xldate):
   tempDate = datetime(1900, 1, 1)
   deltaDays =timedelta(days=int(xldate)-2)
   TheTime = (tempDate + deltaDays )
   return TheTime.strftime("%m/%d/%Y")

>>> xldate_to_datetime(42290)
'10/13/2015'
francisco sollima
  • 7,952
  • 4
  • 22
  • 38
rofls
  • 4,993
  • 3
  • 27
  • 37