Skip to content

Instantly share code, notes, and snippets.

@OmarArain
Created April 3, 2014 17:48
Show Gist options
  • Save OmarArain/9959241 to your computer and use it in GitHub Desktop.
Save OmarArain/9959241 to your computer and use it in GitHub Desktop.
convert Excel serial date to python datetime.datetime
import datetime
def xldate_to_datetime(xldate):
temp = datetime.datetime(1900, 1, 1)
delta = datetime.timedelta(days=xldate)
return temp+delta
@zongokevin
Copy link

Hi, It didtn't work for me to read the Excel file on windows, I make those changes to be able to read it
def xldate_to_datetime(xldate):
temp = datetime.datetime(1899, 12, 30)
delta = datetime.timedelta(days=xldate)
return temp+delta

@alistairwalsh
Copy link

alistairwalsh commented Feb 21, 2018

I found the same as zongokevin and If you don't mind importing pandas
here's an alternative

import pandas as pd

def convert_excel_time(excel_time):
    '''
    converts excel float format to pandas datetime object
    round to '1min' with 
    .dt.round('1min') to correct floating point conversion innaccuracy
    '''
    
    return pd.to_datetime('1899-12-30') + pd.to_timedelta(excel_time,'D')

@ManojA-Hexaware
Copy link

Thanks a lot guys.It worked for me when I used
temp = datetime.datetime(1899, 12, 30)

@akanik
Copy link

akanik commented Jul 11, 2018

Oh thank you. Saved me.

@davidkwast
Copy link

I found the same as zongokevin and If you don't mind importing pandas
here's an alternative

import pandas as pd

def convert_excel_time(excel_time):
    '''
    converts excel float format to pandas datetime object
    round to '1min' with 
    .dt.round('1min') to correct floating point conversion innaccuracy
    '''
    
    return pd.to_datetime('1899-12-30') + pd.to_timedelta(excel_time,'D')

thanks

@michael135
Copy link

michael135 commented Apr 11, 2019

Great!
@alistairwalsh @oag335

@AdriPhilip
Copy link

Thanks guys !

@santiagollaberia
Copy link

I found the same as zongokevin and If you don't mind importing pandas
here's an alternative

import pandas as pd

def convert_excel_time(excel_time):
    '''
    converts excel float format to pandas datetime object
    round to '1min' with 
    .dt.round('1min') to correct floating point conversion innaccuracy
    '''
    
    return pd.to_datetime('1899-12-30') + pd.to_timedelta(excel_time,'D')

thanks!! It really helped me!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment