0

I want to covert a column with Excel date numbers (float) to datetime, below is the function I am trying to use:

import datetime
datetime.datetime.fromtimestamp(42029.0).strftime('%Y-%m-%d')

Bu the result I got is: '1970-01-01',I don't think it is right, I must missing a constant which should be added to the variable, because in the excel the number 42029.0 represents date: 1/25/2015.

Can anyone please advise how to improve the code?

Peter
  • 10,959
  • 2
  • 30
  • 47
Worst SQL Noob
  • 189
  • 1
  • 5
  • 15

1 Answers1

0

datetime.fromtimestamp() follows the unix convention and expects seconds since '1970-01-01'. The Excel date number for '1970-01-01' is 25569, so you have to subtract this number from your given date number and multiply the result, which is in days, with 86400 (seconds per day):

datetime.datetime.fromtimestamp(
   (42029.0 - 25569) * 86400).strftime('%Y-%m-%d')

'2015-01-25'
Peter
  • 10,959
  • 2
  • 30
  • 47